Marwan Ayman Shawky

Cloud & DevOps Engineer

Linux File Management Made Simple: How to Copy, Move, Delete, and Organize

·Calculating...·
linux

Introduction

So far in this series, you’ve:

Now it’s time to roll up your sleeves and actually work with files and directories. This is where Linux becomes fun — and powerful.

Navigating Files and Directories (short recap)

  • pwd → print working directory

  • ls → list files

  • ls -l → list with details

  • ls -a → show hidden files

  • cd folder_name → change directory

Pro Tip: Use cd - to jump back to your previous directory.

3. Creating Files and Folders

  • I created a folder called "practice" in the home directory using mkdir practice and then entered it to run all the commands for this section.

  • Create empty file:

    touch notes.txt
    

  • Create a directory: (You'll notice that directories are colored in blue)

    mkdir projects
    

  • Create nested directories:

    mkdir -p projects/app/src
    

  • You can see how in both lines we check the first nested directory, and then in the next line, we check another nested one inside projects/app.

4. Copying and Moving Files

  • Copy file:

    cp notes.txt notes2.txt
    

    • Copy directory

      cp -r projects projects2/
      

    • Move file:

      mv notes.txt projects/
      

    • Rename file:

      mv notes2.txt notes3.txt
      

      5. Deleting Files and Directories

      • Delete file:

        rm notes.txt
        

      • Delete multiple files:

        rm notes2.txt notes3.txt
        

      • Delete directory (recursively):

        rm -r projects2/
        

      • Safer delete with confirmation:
        You can confirm your choice by typing either (Yes/No) or (Y/N).

        rm -i notes1.txt
        

⚠️ Warning: Be careful with rm -rf /. It can wipe your system.

6. Viewing File Contents

  • Show whole file: cat notes.txt

  • Scroll through: less notes.txt

  • Show the top lines: head -n 10 notes.txt (you can replace 10 with the number of lines you want from the top)

  • Show bottom lines: tail -n 10 notes.txt

  • Follow logs in real time:

    tail -f /var/log/syslog
    

    7.1 File Globbing & Wildcards

I have prepared a directory to apply our new practices on file globbing and wildcards. Follow the steps in the next screenshots and instructions.

1. Copy all .txt files into a folder

mkdir texts
cp *.txt texts/

2. Move only report1.pdf and report2.pdf

mkdir reports
mv report[1-2].pdf reports/

3. Delete only January–June backups

rm backup_2023-0[1-6]*.tar.gz

💡 In this specific command, the * acts as a wildcard to match any characters that might appear after 2023-0[1-6] and before .tar.gz.

4. View only image files starting with "image"

5. Concatenate text files into one

cat file?.txt > combined.txt

6. Tar archive all PDF reports

tar -czf reports.tar.gz report*.pdf

  • 7.2 File Globbing & Wildcards

Once you’re comfortable with the basics, here are some real-world use cases where globbing saves tons of time:

# 1. Filter files by extension
ls *.pdf
ls *.jpg

# 2. Select files with a specific ending
ls *final.jpg      # only matches image_final.jpg

# 3. Exclude hidden files
ls *               # shows all files, but not dotfiles (like .bashrc)

# 4. Batch rename files (prefix all .txt files)
for f in *.txt; do mv "$f" "old_$f"; done
# file1.txt → old_file1.txt

# 5. Copy multiple types at once
cp *. media/

# 6. Match files with character ranges
ls file[0-9].txt   # matches file1.txt, file2.txt (but not file10.txt)

# 7. View rotated log files
ls /var/log/syslog*   # syslog, syslog.1, syslog.2.gz ...

# 8. Backup important configs with brace expansion
cp /etc/ ~/configs/

Real-World DevOps Examples

  • Archive all logs:

    tar -czf logs.tar.gz *.log
    
  • Remove old backup files:

    rm backup_2023-0[1-6]*.tar.gz
    
  • Copy multiple configs at once:

    cp /etc/ ~/configs/
    

🏆 Mini Challenge

Try these on your own:

  • Rename all .jpg files by prefixing them with img_.

  • Copy all .pdf and .doc files into a new folder called documents/.

  • Delete all backup files except the ones from July onward.

8. Hidden Files & Dotfiles

  • Files starting with . are hidden by default.

  • Example: .bashrc, .gitignore, .ssh/config.

  • Show them with:

    ls -a
    

💡 Hidden files usually store configurations.

9. Absolute vs Relative Paths

  • Absolute path → starts from / (root) — replace the placeholder with your username

    cd /home/<your-username>/projects
    

  • Relative path → relative to your current directory

    cd ../projects
    

💡 Pro Tip: Use ~ for your home directory:

cd ~/Downloads # Replace Downloads with any folder your have in username home folder.

10. Links

  • Hard link: another name for the same file

    ln file.txt file_hard.txt 
    # Replace file.txt with the original filename / Replace file_hard.txt with the new link name.
    

  • Identical: The hard link is indistinguishable from the original file. Both have the same size, permissions, and owner, and both point to the same data.

  • Durability: If you delete the original file, the hard link remains valid because it's pointing to the data itself, not the file's name. The data is only truly deleted when the last hard link to its inode is removed.

  • Limitations: Hard links cannot link to directories to prevent recursive loops, and they cannot cross different file systems or partitions.

  • Soft link (symlink): shortcut to another file

    ln -s /var/log/syslog syslog_link # It can work with files and directories!
    

    • 1 - We use soft links with files.

    • 2 - We use soft links with directories.

  • Check links with:

    ls -l
    

11. Practice Challenge

  • Create a playground/ directory

  • Inside it, create 3 text files and 2 folders

  • Copy the files into one folder

  • Move one file to another folder

  • Delete one folder with its contents

  • Use wildcards to list only .txt files

  • Bonus: Create a symlink to one of the file

Wrap-Up & Next Steps

File management is the backbone of everyday Linux work. You now know how to create, move, copy, and delete files, explore contents, and even use wildcards to handle many files at once.

In the next article, we’ll go one step further: Installing and Managing Software (apt, yum, dnf) — the gateway to customizing your Linux system.

"Mastering files is mastering Linux. Every pro started here — and so have you."

Linux Globbing Cheat Sheet

PatternMatches Example
*Any number of characters → *.txtfile.txt, notes.txt
?Single character → file?.txtfile1.txt, fileA.txt
[abc]Any one character in set → file[123].txtfile1.txt
[a-z]Range of characters → file[a-c].txtfilea.txt, fileb.txt
``Alternatives → *.photo.jpg, image.png
! (inside [])Negation → file[!0-9].txt → matches letters, not numbers

👉 You can follow this series through this customized link: Linux Series