$ Linux Commands

File Management — Working with files and directories

cat File Management

Concatenate and display file contents

cat [OPTIONS] FILE...
cat file.txt
cat file1.txt file2.txt > combined.txt
cat -n file.txt
-n number all output lines
-A show non-printing characters
cd File Management

Change the current directory

cd [PATH]
cd /home/alice
cd ~
cd ..
cd -

cd - returns to the previous directory. cd ~ goes to your home directory.

cp File Management

Copy files or directories

cp [OPTIONS] SOURCE DEST
cp file.txt backup.txt
cp -r /src/dir /dest/dir
cp -i file.txt /tmp/
-r copy directories recursively
-i prompt before overwriting
-u copy only when source is newer
-v verbose output
find File Management

Search for files in a directory hierarchy

find PATH [OPTIONS] [EXPRESSION]
find . -name "*.py"
find /var/log -name "*.log" -mtime -7
find . -type d
find . -size +10M
-name match by filename
-type f=file, d=directory
-mtime modified N days ago
-size match by size
-exec run command on results

Use -exec rm {} \; to delete found files, or pipe to xargs for efficiency.

head File Management

Output the first lines of a file

head [OPTIONS] FILE
head file.txt
head -n 20 file.txt
-n N print first N lines (default 10)
less File Management

View file contents one screen at a time

less FILE
less /var/log/syslog
less +F /var/log/nginx/error.log
+F follow mode (like tail -f)

Press q to quit, / to search, n for next match, G to jump to end.

ln File Management

Create hard or symbolic links

ln [OPTIONS] TARGET LINK_NAME
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/myapp
ln file.txt hardlink.txt
-s create a symbolic (soft) link

Symlinks point to a path; hard links point to the same inode. Use -s for most cases.

ls File Management

List directory contents

ls [OPTIONS] [PATH]
ls
ls -la
ls -lh /var/log
ls -lt
-l long listing format
-a show hidden files
-h human-readable sizes
-t sort by modification time
-r reverse sort order
-S sort by file size

Use ls -la to see all files including hidden ones with full details.

mkdir File Management

Create directories

mkdir [OPTIONS] DIR...
mkdir mydir
mkdir -p /var/myapp/logs
mkdir -m 755 public
-p create parent directories as needed
-m set permissions (octal)
mv File Management

Move or rename files and directories

mv [OPTIONS] SOURCE DEST
mv old.txt new.txt
mv file.txt /tmp/
mv -i file.txt /tmp/
-i prompt before overwriting
-v verbose output

mv is also how you rename files — just move to a new name in the same directory.

pwd File Management

Print working directory (current path)

pwd
pwd
rm File Management

Remove files or directories

rm [OPTIONS] FILE...
rm file.txt
rm -rf /tmp/mydir
rm -i *.log
-r remove directories recursively
-f force, no prompts
-i prompt before each removal
-v verbose

Be careful with rm -rf — it deletes without confirmation and cannot be undone.

tail File Management

Output the last lines of a file

tail [OPTIONS] FILE
tail file.txt
tail -n 50 /var/log/syslog
tail -f /var/log/nginx/access.log
-n N print last N lines (default 10)
-f follow: output appended data as file grows

tail -f is essential for watching live log output.

touch File Management

Create an empty file or update timestamps

touch FILE...
touch newfile.txt
touch file1.txt file2.txt