File and Directory Operations
Table of Contents
ls
β Lists the contents of a directory.ls ls -l # Long listing format ls -a # List all files, including hidden ones
cd
β Changes the current directory.cd /path/to/directory cd .. # Go up one directory cd ~ # Go to the home directory
mkdir
β Creates a new directory.mkdir new_directory
rmdir
β Removes an empty directory.rmdir directory_name
cp
β Copies files or directories.cp source_file destination cp -r source_directory destination_directory # Copy directories recursively
mv
β Moves or renames files and directories.mv old_name new_name mv file_name /path/to/destination/
rm
β Removes files or directories.rm file_name rm -r directory_name # Remove directories recursively
touch
β Creates an empty file or updates the timestamp of an existing file.touch file_name
File Viewing & Manipulation
less
β Allows you to view file contents page by page.less file_name
head
β Shows the first 10 lines of a file (default).head file_name head -n 5 file_name # Show the first 5 lines
tail
β Shows the last 10 lines of a file (default).tail file_name tail -n 5 file_name # Show the last 5 lines
grep
β Searches for patterns within files.grep 'search_term' file_name grep -r 'search_term' /path/to/directory # Search recursively in directories
Permissions & Ownership
chmod
β Changes file permissions.chmod 755 file_name # Gives read, write, execute permissions to the owner and read, execute to others chmod +x script.sh # Make file executable
chown
β Changes the file owner and group.chown user:group file_name
umask
β Sets default file creation permissions.umask 022 # Sets default permissions to 755 for directories and 644 for files
Process Management
ps
β Displays the currently running processes.ps ps aux # Show all processes
top
β Displays real-time system processes and resource usage.top
kill
β Terminates a process by its PID.kill process_id kill -9 process_id # Forcefully kill a process
htop
β Interactive process viewer (requires installation).htop
System Information
df
β Shows disk space usage.df -h # Human-readable format
du
β Shows disk usage for files and directories.du -h /path/to/directory
free
β Displays memory usage.free -h # Human-readable format
uname
β Shows system information.uname -a # Display all system info
uptime
β Shows how long the system has been running.uptime
whoami
β Displays the current logged-in user.whoami
hostname
β Displays or sets the system’s hostname.hostname
lscpu
β Displays CPU architecture information.lscpu
Network Commands
ping
β Tests connectivity to a host.ping google.com
ifconfig
β Displays network interface information (may require net-tools installation on some systems).ifconfig
ip
β Configures network interfaces and routing.ip addr show # Show IP addresses of network interfaces ip route show # Show routing table
curl
β Fetches data from a URL.curl https://example.com
wget
β Downloads files from the web.wget https://example.com/file.zip
Package Management
apt-get
(for Debian/Ubuntu-based distributions) β Installs, updates, or removes software packages.sudo apt-get update # Update package list sudo apt-get install package # Install a package sudo apt-get remove package # Remove a package
yum
(for RedHat/CentOS-based distributions) β Installs, updates, or removes software packages.sudo yum update # Update package list sudo yum install package # Install a package sudo yum remove package # Remove a package
File Compression
tar
β Archives or extracts files.tar -czvf archive_name.tar.gz /path/to/directory # Create a compressed archive tar -xzvf archive_name.tar.gz # Extract a compressed archive
zip
β Compresses files into a zip archive.zip archive_name.zip file1 file2
unzip
β Extracts a zip archive.unzip archive_name.zip
Miscellaneous
echo
β Prints a message or variables to the terminal.echo "Hello, World!"
date
β Displays or sets the system date and time.date
alias
β Creates an alias for a command.alias ll='ls -la' # Create a shortcut for 'ls -la'
history
β Shows the command history.history
clear
β Clears the terminal screen.clear
These are just a few of the many powerful commands in Linux, but they cover most of the common operations you’ll perform daily.