-
Present Working Directory:
pwd -
List Content:
ls- To Show Hidden files:
ls -a - To Show files in-order of creation:
ls -lt - To Show files in the reverse order of creation:
ls -ltr
- To Show Hidden files:
-
Make Directory
- Make a New Directory:
mkdir <file or directory name> - Make a Multiple Directory:
mkdir <file1> <file2> <file3> - Make a Directory without going inside the directory:
mkdir <filename>/<newfile> - Create a child directory with simultaneously creating a parent directory:
mkdir -p <newfile1>/<newfile2> - Create a file under each directory by the name
capital.txwithout going inside the directory:touch /home/username/countries/{USA,UK}/capital.txt - Add the capital cities name in the file Using
echocommand:echo "Washington, D.C" > /home/username/countries/USA/capital.txt && echo "London" > /home/username/countries/UK/capital.txt
- Make a New Directory:
-
Change Directory
- Changes the current directory to the user's home directory:
cd - Changes the current directory to the parent directory of the current working directory:
cd .. - Absolute Path: Full path from root, starts with
/, like:cd /home/username - Relative Path: Path relative to current directory, doesn't start with
/, like:cd ../myfile
- Changes the current directory to the user's home directory:
-
pushd: Adds a directory to the directory stack and changes the current directory. -
popd: Removes directories from the stack and changes the current directory to the previous directory in the stack.
-
Move File or Directory:
mv /home/user/europe/pakistan /home/user/asiaormv europe/pakistan asia/ -
Rename File or Directory:
mv Asia/Pkistn /Asia/Pakistan -
Copy File:
cp asia/pakistan/country.txt asia/india -
Remove Directory:
rmdir <filenanme> -
Remove File:
rm asia/pakistan/country.txt -
Copies directories and their contents recursively:
cp -r source_directory target_directorylike:cp -r /home/user/working /home/user/backup
-
Working with Files & Directory:
CatCommand- Display File Content:
cat <filename> - Concatenate Multiple Files:
cat file1 file2 > combined_file - Append to a File:
cat file1 >> file2 - Create a new file and add text:
cat > newfile.txt
- Display File Content:
-
Create a file using
touchCommand- Create a new empty file:
touch <filename> - Create a new empty file without navigating into the directory:
touch /home/user/asia/test.txt>
- Create a new empty file:
-
To display a line of text or a string that is passed as an argument:
echoCommandecho [options] [argument]like:echo "Hello World"-
-n: Do not output the trailing newline.-e: Enable interpretation of backslash escapes.-E: Disable interpretation of backslash escapes (default).
-
Text Editor:
vi&nanocammand-
Open a file in Vim:
vi <file_name>- Modes:
i: Insert modeEsc:Normal mode
- Navigation:
h,j,k,l: Move left, down, up, and rightgg: Go to the beginning of the fileG: Go to the end of the filew: Move to the beginning of the next wordb: Move to the beginning of the previous word
- Editing Text:
i: Insert before the cursora: Insert after the cursoro: Open a new line below the cursor and enter Insert Modedd: Delete the current lineyy: Yank (copy) the current linep: Paste the yanked or deleted content after the cursor
- Save and Quit:
:w: Save:q: Quit:wq: Save and quit:q!: Quit without saving
- Modes:
-
Open a file in nano:
nano <filename>- Navigation:
Ctrl + A: Start of lineCtrl + E: End of line
- Editing:
Ctrl + K: Cut lineCtrl + U: Paste
- Save and Quit:
Ctrl + O: Save (Enter to confirm)Ctrl + X: Exit
- Navigation:
-
-
Displaying File Content One Page at a Time: Pages
-
morecommand displays the content of a file one screen at a time:more <file.txt> -
lesscommand is more powerful than more and allows for backward movement in the file as well as forward movement:less <file.text> -
- [Spacebar]: Move to the next screen.
- [Enter]: Move one line down.
- [b]: Move one screen back (if supported by the terminal).
-
- [Up Arrow (↑)]: Move one line up.
- [Down Arrow (↓)]: Move one line down.
- [/]: Search test.
-
-
Viewing a file or directory sizes (disk usage):
- Summarize total size and show size in kilobytes:
du -sk test.img - Summarize total size, show size in Megabytes and human-readable format:
du -sh test.img - Long format and human-readable format:
ls -lh test.img
- Summarize total size and show size in kilobytes:
-
Archiving Files:
- Archiving more than one file:
tar -cf test.tar file1 file2 - Show long list content:
tar -tf test.tar - Extract content:
tar -xf test.tar - Compress the file:
tar -zcf test.tar file1 file2
- Archiving more than one file:
-
Compressing Filess:
bzip2: High compression ratio, moderate speed.gzip: Fast compression/decompression, lower compression ratio.xz: Highest compression ratio,
-
Un-compressing:
bunzip2 <filename>gunzip <filename>unxz <filename>
-
How to read a compressed file without un-compressing:
bzcat <filename>zact <filename>xzcat <filename>
-
Searching/Finding for a File or Directory names -
locate:- For finding file names:
locate <filename> - For Case-insensitive search:
locate -i <filename> - For Limit number of results:
locate -n 10 <filename> - For Update the database(if locate command doesn't work):
sudo updatedb
- For finding file names:
-
Searching/Finding text patterns within files -
grep:- For finding patterns within files:
grep "pattern" <filename> - For Search in multiple files:
grep "pattern" <file1> <file2> - Case-insensitive search:
grep -i "pattern" <filename> - Print a line using pattern:
grep "pattern" <file1.text> - Display line numbers:
grep -n "pattern" <filename> - Search for whole words:
grep -w "word" <filename> - Invert match (show lines that do not match):
grep -v "pattern" <filename> - Count matching lines:
grep -c "pattern" <filename> - Using grep with other commands:
command | grep "pattern" - Recursive search through directories(When you don't know the file content):
grep -r "pattern" directory/ - Invert match (show lines that do not match the pattern):
grep -v "pattern" <filename> - Treat binary files as text (useful if you need to search binary files):
grep -a "pattern" <filename> - Show byte offset of matching lines:
grep -b "pattern" <filename> - Combining options:
grep -r -w -v "pattern" directory/ - To check vendor for the ethernet controller used in sys:
ispc: | grep EthernetORcd /sys/class/net/eth0/devices
- For finding patterns within files:
-
Documentation and Get help Commands:
- Command to describe how a command name is interpreted, identify whether a command is internal or external:
type grep - Command for displaying a brief description of other commands"
whatis echo - Manual command to view detailed documentation (man pages) for other commands:
man echo - Command-line option to display a brief help message with usage information for a command:
echo --help - Command for searching the manual page names and descriptions for a keyword:
apropos modpr
- Command to describe how a command name is interpreted, identify whether a command is internal or external:
-
Piping:
command1 | command2, e.g.ls -l | grep ".txt"
-
Kernel Version:
uname
-
Kernel Version Number:
uname -r
-
systemctlCommands - interact with the systemd system and service manager:- Start a service:
sudo systemctl start service_name - Stop a service:
sudo systemctl stop service_name - Restart a service:
sudo systemctl restart service_name - Reload a service (for services that support reloading without stopping):
sudo systemctl reload service_name - To check init process in sys:
systemctl --version - To check run level:
runlevel - To check default systemd target set:
systemctl get-default - To change it to multi-user.target:
sudo systemctl set-default multi-user.target - File type is
firefox.debin/root:sudo file /root/firefox.deb
- Start a service:
-
Input Redirection - allows users to control the input and output of commands:
-
Input Redirection:
- Redirect input from a file:
command < input_file
- Redirect input from a file:
-
Output Redirection:
- Redirect output to a file (overwrite):
command > output_file, e.g.echo $SHELL > shell.txt - Redirect output to a file (append):
command >> output_file, e.g.echo "This is a bash file" >> shell/txt
- Redirect output to a file (overwrite):
-
Error Redirection:
- Redirect error output to a file (overwrite):
command 2> error_file, e.g.cat missing_file 2 > error.txt - Redirect error output to a file (append):
command 2>> error_file, e.g.cat missing_file 2 >> error.txt
- Redirect error output to a file (overwrite):
-
To avoid print error on screen:
cat missing_file 2 > /dev/null∴ /dev/null = dum bucket
-
-
IP:
- To Find IP:
IP address
- To Find IP:
-
Ping:
- Used to test the reachability of a host on an IP network:
ping <hostname_or_ip_address>
- Used to test the reachability of a host on an IP network:
-
Ping6:
- Used to test the reachability of a host using IPv6:
ping6 <hostname_or_ip_address>
- Used to test the reachability of a host using IPv6:
-
Hostname:
- Displays or sets the system's hostname:
hostnameOR Set a New Hostname:sudo hostname newhostname
- Displays or sets the system's hostname:
-
nslookup:
- Queries the DNS to obtain domain name or IP address mapping.:
nslookup <hostname_or_ip_address>
- Queries the DNS to obtain domain name or IP address mapping.:
-
dig:
- More advanced DNS querying tool:
dig <hostname>
- More advanced DNS querying tool:
-
route:
- Displays or modifies the IP routing table:
route- To add a route:
sudo route add -net <network> netmask <netmask> gw <gateway>
- To add a route:
- Displays or modifies the IP routing table:
-
ip link:
- Displays or modifies network interfaces:
ip link show- To bring an interface up:
sudo ip link set <interface> up - To bring an interface down:
sudo ip link set <interface> down
- To bring an interface up:
- Displays or modifies network interfaces:
-
To Find DNS Name
cat /etc/resolv.conf
-
File that is responsible for host file-based DNS
/etc/host
-
Change the order to DNS & Hosts
sudo vim /etc/nsswitch.conf, thenHosts = dnsfiles
-
To change DNS nameserver
sudo vim /etc/resolv.conf
- Testcase init
- Check Interface:
ip link - Check DNS Resolution:
nslookup <ip_address> - Check Connectivity:
ping <ip_address> - Check Route:
traceroute <ip_address> - Check Server:
netstat -an | grep 80 | grep -i LISTEN - Check again Interface:
ip link
- Create a User:
sudo useradd username - Set Password for User:
sudo passwd username - Display Current User:
whoami - Delete a User:
sudo userdel username - Delete a User and Their Home Directory:
sudo userdel -r username - To view
userstored information:cat /etc/passwd
note:
- To Check information about the user:
id <username> - To view the last user who is currently logged- in:
who - To view all logged-in users:
last
- Create a Group:
sudo groupadd groupname - Add a User to a Group:
sudo usermod -aG groupname username - List Groups for a User:
groups username - To view
groupstored information:cat /etc/group
- Runs a command as the root user:
sudo <command> - Switch to Root User:
sudo suORsu -
- List System Accounts:
awk -F: '($3 < 1000) {print $1}' /etc/passwd
- View Service Accounts:
awk -F: '($3 < 1000) {print $1}' /etc/passwd - To view the
sudoaccount stored information:cat /etc/sudoers - To edit sudoers File:
visudo
- View User Information:
cat /etc/passwd- Displays user information for users starting with
username:cat /etc/passwd | grep '^username'
- Displays user information for users starting with
- View Encrypted Password Information:
sudo grep '^username' /etc/shadow - View Group Information:
grep '^username' /etc/group