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
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/
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
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
less
File Management
View file contents one screen at a time
less FILE
less /var/log/syslog less +F /var/log/nginx/error.log
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
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
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
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/
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
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
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