Unix – Important Commands with Useage #
1. File and Directory Management Commands #
ls #
- Usage: Lists files and directories.
- Syntax:
ls [options] [path]
- Example:
ls
: List files in the current directory.ls -l
: List files in long format with details like permissions, owner, size, etc.ls -a
: List all files, including hidden files.
cd #
- Usage: Changes the current directory.
- Syntax:
cd [directory]
- Example:
cd /home/user
: Change to the specified directory.cd ..
: Move to the parent directory.cd ~
: Move to the home directory.
pwd #
- Usage: Prints the current working directory.
- Syntax:
pwd
- Example:
pwd
mkdir #
- Usage: Creates a new directory.
- Syntax:
mkdir [options] directory_name
- Example:
mkdir my_folder
: Creates a directory namedmy_folder
.mkdir -p dir1/dir2
: Creates nested directories.
rm #
- Usage: Removes files or directories.
- Syntax:
rm [options] file_or_directory
- Example:
rm file.txt
: Deletesfile.txt
.rm -r folder
: Deletes a directory and its contents recursively.
rmdir #
- Usage: Removes an empty directory.
- Syntax:
rmdir directory_name
- Example:
rmdir empty_folder
cp #
- Usage: Copies files or directories.
- Syntax:
cp [options] source destination
- Example:
cp file1.txt file2.txt
: Copiesfile1.txt
tofile2.txt
.cp -r folder1 folder2
: Copiesfolder1
tofolder2
recursively.
mv #
- Usage: Moves or renames files or directories.
- Syntax:
mv [options] source destination
- Example:
mv file1.txt file2.txt
: Renames or movesfile1.txt
tofile2.txt
.
2. File Viewing Commands #
cat #
- Usage: Displays the content of a file.
- Syntax:
cat [options] file
- Example:
cat file.txt
: Displays the content offile.txt
.cat file1.txt file2.txt > merged.txt
: Concatenates two files into a new file.
more #
- Usage: Displays file content page by page.
- Syntax:
more [file]
- Example:
more file.txt
less #
- Usage: Displays file content, allowing navigation.
- Syntax:
less [file]
- Example:
less file.txt
head #
- Usage: Displays the first few lines of a file.
- Syntax:
head [options] file
- Example:
head -n 10 file.txt
(Shows the first 10 lines).
tail #
- Usage: Displays the last few lines of a file.
- Syntax:
tail [options] file
- Example:
tail -n 10 file.txt
(Shows the last 10 lines).
3. File Permission and Ownership Commands #
chmod #
- Usage: Changes file permissions.
- Syntax:
chmod [options] mode file
- Example:
chmod 755 file.txt
: Sets read, write, and execute for owner; read and execute for group and others.
chown #
- Usage: Changes file ownership.
- Syntax:
chown [owner][:group] file
- Example:
chown user:group file.txt
chgrp #
- Usage: Changes group ownership.
- Syntax:
chgrp [group] file
- Example:
chgrp group file.txt
4. Process Management Commands #
ps #
- Usage: Displays information about active processes.
- Syntax:
ps [options]
- Example:
ps
: Displays processes for the current shell.ps -ef
: Displays all running processes.
top #
- Usage: Shows real-time processes and system resource usage.
- Syntax:
top
kill #
- Usage: Terminates a process by PID.
- Syntax:
kill [signal] PID
- Example:
kill -9 12345
jobs #
- Usage: Lists background jobs.
- Syntax:
jobs
fg #
- Usage: Brings a background job to the foreground.
- Syntax:
fg [job_id]
bg #
- Usage: Resumes a suspended job in the background.
- Syntax:
bg [job_id]
5. Networking Commands #
ping #
- Usage: Checks connectivity to a host.
- Syntax:
ping [options] host
- Example:
ping google.com
netstat #
- Usage: Displays network connections and statistics.
- Syntax:
netstat [options]
curl #
- Usage: Transfers data from or to a server.
- Syntax:
curl [options] URL
- Example:
curl https://example.com
wget #
- Usage: Downloads files from the web.
- Syntax:
wget [options] URL
- Example:
wget https://example.com/file.txt
scp #
- Usage: Copies files between hosts.
- Syntax:
scp [options] source destination
- Example:
scp file.txt user@host:/path/to/destination
ssh #
- Usage: Logs into a remote machine securely.
- Syntax:
ssh user@host
- Example:
ssh user@192.168.1.1
6. Disk Usage Commands #
df #
- Usage: Displays disk space usage.
- Syntax:
df [options]
- Example:
df -h
(Shows human-readable format).
du #
- Usage: Displays directory or file space usage.
- Syntax:
du [options] [path]
- Example:
du -sh folder/
7. Search and Filter Commands #
grep #
- Usage: Searches for a pattern in files.
- Syntax:
grep [options] pattern file
- Example:
grep 'hello' file.txt
: Searches for ‘hello’ infile.txt
.grep -r 'error' /var/logs
: Searches recursively for ‘error’ in/var/logs
.
find #
- Usage: Searches for files and directories.
- Syntax:
find [path] [options]
- Example:
find . -name "*.txt"
: Finds all.txt
files in the current directory.find /home -size +100M
: Finds files larger than 100MB.
awk #
- Usage: Processes and analyzes text.
- Syntax:
awk 'pattern {action}' file
- Example:
awk '{print $1}' file.txt
: Prints the first column of a file.
sed #
- Usage: Edits text in a file or stream.
- Syntax:
sed 'script' file
- Example:
sed 's/old/new/g' file.txt
: Replaces ‘old’ with ‘new’.
8. Archiving and Compression Commands #
tar #
- Usage: Archives files and directories.
- Syntax:
tar [options] archive_name file_or_directory
- Example:
tar -cvf archive.tar folder
gzip #
- Usage: Compresses files.
- Syntax:
gzip [file]
- Example:
gzip file.txt
unzip #
- Usage: Extracts ZIP files.
- Syntax:
unzip file.zip
This list provides an essential toolkit for working effectively in a Unix environment. Mastering these commands can significantly enhance your productivity and proficiency!