Skip to content
5 min read·Lesson 5 of 10

Files and Directories

Create, copy, move, delete, and search files and directories using core Linux commands.

Working with files is the core of Linux administration. Whether you're deploying configs, managing logs, or setting up a server, these commands are your daily toolkit.

Creating Files and Directories

mkdir logs                  # create a directory
mkdir -p app/config/nginx   # create nested directories
touch README.md             # create empty file (or update timestamp)
echo "Hello" > notes.txt   # create file with content

Copying and Moving

cp file.txt backup.txt          # copy file
cp -r src/ dst/                 # copy directory recursively
mv old-name.txt new-name.txt    # rename file
mv file.txt /tmp/               # move to different directory
mv dir/ /opt/app/               # move entire directory

Deleting Files and Directories

rm file.txt            # delete a file
rm -i file.txt         # prompt before deleting
rm -r old-project/     # delete directory recursively
rm -rf /tmp/build/     # force-delete without prompts

Warning: rm -rf permanently deletes files — there is no Recycle Bin or undo. Always double-check the path before running it, especially as root.

Viewing Directory Contents

ls                    # basic listing
ls -l                 # long format with permissions, size, date
ls -la                # include hidden files (starting with .)
ls -lh                # human-readable sizes (KB, MB)
ls -lt                # sort by modification time (newest first)
tree /etc/nginx       # visual tree view (install with apt install tree)

Working with File Content

cat nginx.conf             # print entire file
less nginx.conf            # page through file
head -5 access.log         # first 5 lines
tail -10 error.log         # last 10 lines
tail -f /var/log/syslog    # follow file as it grows
wc -l server.log           # count lines in file

Finding Files

find — powerful, flexible, real-time search

find / -name "nginx.conf"            # find by name
find /var/log -name "*.log"          # wildcard match
find /home -user alice               # files owned by alice
find /tmp -mtime -1                  # modified in last 24 hours
find /opt -size +100M                # files larger than 100 MB
find . -type d                       # find only directories
find . -type f -name "*.sh" -exec chmod +x {} ;

locate — fast indexed search

locate nginx.conf    # instant search (uses a pre-built database)
sudo updatedb        # update the database

Wildcards

ls *.log         # all files ending in .log
ls access*       # all files starting with "access"
ls report?.txt   # report1.txt, report2.txt (? matches single char)
rm temp[0-9].txt # delete temp0.txt through temp9.txt

Symbolic Links

A symbolic link (symlink) is a pointer to another file or directory — like a shortcut on Windows:

ln -s /opt/nginx/conf/nginx.conf /etc/nginx/nginx.conf
# /etc/nginx/nginx.conf now points to /opt/nginx/conf/nginx.conf

ls -l /etc/nginx/nginx.conf
# lrwxrwxrwx 1 root root 30 ... /etc/nginx/nginx.conf -> /opt/nginx/conf/nginx.conf

Symlinks are used extensively in Linux to make versioned software accessible at a stable path (e.g., /usr/bin/python3/usr/bin/python3.12).

Archiving and Compressing

tar -czf archive.tar.gz /etc/nginx     # create gzipped archive
tar -xzf archive.tar.gz               # extract
tar -tzf archive.tar.gz               # list contents
zip -r site.zip ./public/             # create zip
unzip site.zip

Next, you'll learn about file permissions — the system that controls who can read, write, and execute each file.

Key Takeaways

  • mkdir, touch, cp, mv, and rm are the five commands you'll use constantly.
  • rm -rf is permanent — there is no Recycle Bin on Linux.
  • find and locate let you search for files by name, size, date, or content.
  • Wildcards (* and ?) let you match multiple files in one command.
  • Hard links and symbolic links both provide alternative paths to the same data.

Test your knowledge

Try exam-style practice questions to reinforce what you've learned.

Practice Questions →