Some basic Unix commands commonly used for file and directory management, system information, and other tasks are:
1. File and Directory Operations
ls
: List the contents of a directory.ls # List files in the current directory ls -l # List files with detailed information (permissions, owner, etc.) ls -a # List all files, including hidden files (those starting with a dot)
cd
: Change the current directory.cd /path/to/directory # Change to a specific directory cd .. # Move up one level (to the parent directory) cd ~ # Change to the home directory
pwd
: Print the current working directory.pwd
mkdir
: Create a new directory.mkdir new_directory
rmdir
: Remove an empty directory.rmdir directory_name
rm
: Remove files or directories.rm file_name # Remove a file rm -r directory_name # Remove a directory and its contents rm -f file_name # Force remove a file (without confirmation)
cp
: Copy files or directories.cp source_file destination_file # Copy a file cp -r source_directory destination_directory # Copy a directory and its contents
mv
: Move or rename files or directories.mv source_file destination_file # Rename or move a file mv source_directory destination_directory # Move a directory
2. Viewing and Editing Files
cat
: Display the contents of a file.cat file_name
less
: View the contents of a file one page at a time.less file_name
more
: Similar toless
, but with fewer navigation options.more file_name
head
: Display the first few lines of a file.head file_name # By default, shows the first 10 lines head -n 20 file_name # Display the first 20 lines
tail
: Display the last few lines of a file.tail file_name # By default, shows the last 10 lines tail -n 20 file_name # Display the last 20 lines
nano
orvim
: Text editors for editing files.nano file_name # Open file in nano editor vim file_name # Open file in vim editor
3. File Permissions
chmod
: Change file permissions.chmod 755 file_name # Give read, write, and execute permissions to the owner, and read/execute to others chmod +x file_name # Add execute permission to a file
chown
: Change the owner and group of a file or directory.chown user:group file_name # Change owner and group of a file
chgrp
: Change the group ownership of a file or directory.chgrp group file_name # Change group of a file
4. Process Management
ps
: Display information about running processes.ps # Show processes running in the current terminal ps aux # Show all running processes
top
: Display real-time system resource usage and processes.top
kill
: Terminate a running process by its PID (Process ID).kill PID # Terminate the process with the given PID kill -9 PID # Forcefully terminate the process
bg
: Resume a job in the background.bg job_number
fg
: Bring a background job to the foreground.fg job_number
5. System Information
uname
: Display system information.uname -a # Display all information about the system (kernel version, etc.) uname -r # Display the kernel version
df
: Display disk space usage.df # Display the space available on all mounted filesystems df -h # Display human-readable output (in MB, GB, etc.)
du
: Estimate file and directory space usage.du file_name # Display the size of a file du -sh directory_name # Display the size of a directory (human-readable format)
free
: Display memory usage.free # Show free and used memory free -h # Show memory in a human-readable format
uptime
: Display how long the system has been running.uptime
whoami
: Display the current logged-in user.whoami
6. Searching and Finding Files
find
: Search for files in a directory hierarchy.find /path/to/search -name "filename" # Search for a file by name find /path/to/search -type f # Search for regular files find /path/to/search -type d # Search for directories
grep
: Search for a pattern in a file.grep "pattern" file_name # Search for a pattern in a file grep -r "pattern" directory # Search recursively in a directory
locate
: Find the location of a file by name (faster thanfind
).locate filename
7. Networking
ping
: Send ICMP packets to test network connectivity.ping www.example.com # Ping a website
ifconfig
orip a
: Display network interface configurations.ifconfig # Show network interfaces (older command) ip a # Show network interfaces (recommended in modern Linux)
netstat
: Display network connections, routing tables, and interface statistics.netstat # Display active network connections netstat -tuln # Display listening ports
8. File Compression
tar
: Create or extract compressed archive files.tar -cvf archive.tar directory_name # Create a tar archive tar -xvf archive.tar # Extract a tar archive tar -czvf archive.tar.gz directory_name # Create a compressed tarball (.tar.gz) tar -xzvf archive.tar.gz # Extract a compressed tarball
gzip
: Compress files using the gzip algorithm.gzip file_name # Compress a file gunzip file_name.gz # Decompress a file
These are just a few of the basic Unix commands to help you navigate and perform common tasks in a Unix-like environment.