0% found this document useful (0 votes)
6 views

Linux_commands_cheat_sheet

Uploaded by

gmyymw8dqw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Linux_commands_cheat_sheet

Uploaded by

gmyymw8dqw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

•Crucial Linux Commands for Daily Use•

-> Common commands-

whoami: displays the username of currently logged-in user.

history: list all your previously executed commands

systemctl: used to control the system and service manager.


sudo systemctl start service_name
sudo systemctl stop service_name
sudo systemctl restart service_name

sudo: permitted user to execute a command as the superuser (root)


Ex- sudo “ command “

Switch to root user


Ex- sudo -i

id: used to display user and group information for the current user or a specified username
Ex- id
id username

Install something:
For Debian based system-
Ex- sudo apt-get install package_name –y
-y so you don't have to manually type yes

For Red hat based system-


Cents os 7 and earlier fedora
Ex- sudo yum install package_name

Cents os 8 and later fedora


Ex- sudo dnf install package_name

ssh: securely connect to a remote host over a network


Ex- ssh user@remote_host
ssh -p port_number user@remote_host
ssh -i /path/to/private_key user@remote_host

echo: displays line os text or variable value


Ex- echo "Hello, World!"
variable_name="Hello!!"
echo $variable_name

clear: to clear the terminal screen


Ex- clear

useradd- to add new user


Ex- sudo useradd -m username
sudo useradd -m -d /home/username -s /bin/bash username
sudo useradd -m -c "Avatr roku" -G sudo,docker username
-d /home/username: Specify the user's home directory.
-m: Create the home directory if it does not exist.
-c "Full Name": Add a comment (typically the full name).

Set password for it


Ex- sudo passwd username

userdel: to delete a user


Ex- userdel username

exit: to terminate current shell session


Ex- exit

free –m: shows you the memory utilization

df –h: hard disk partition utilization

For help use --help


-> File and Directory Management

ls: List directory contents.


Ex- ls
ls -l
ls -l /path/to/directory

For detailed view of list


ls –lrth or ls -lrt

pwd: displays your present working directory

cd: Change directory


Ex- cd /path/to/directory

One previous dir


Ex- cd ..

Jump to second previous dir


Ex- cd ../../..

mkdir: Create a new directory.


Ex- mkdir new_directory

rmdir: Remove any directory.


Ex- rmdir my_directory

rm: Remove files or directories.


Ex- rm -rf /path/to/directory_or_file

cp: Copy files or directories.


Ex- cp source_file destination target_destination
mv: Move or rename files or directories.
Ex- mv old_name new_name

mv source_file_destination target_destination

touch: Create an empty file or update the timestamp of an existing file.


Ex- touch new_file

-> Viewing and Editing Files

cat: Concatenate and display file content.


Ex- cat file.txt

Note- you can also create single immutable file with cat command

Ex- cat > file.txt


Add your content
Ctrl+ d to exit

Add comment to existing file


Ex- cat >> file.txt
Add...
Ctrl + d to exit

Move data from one file to another


cat file1.txt > file2.txt

" more "," less ": View file content one page at a time.
Ex- less file.txt

" head " , " tail ": View the beginning or end of a file.
Ex- tail -n 10 file.txt

nano, vi, vim: Text editors.


Ex- nano file.txt
vi file.txt
vim file.txt

touch: creates empty file


Ex- touch file.txt
(File extension can be of your interest)

Can create multiple file as well


Ex- touch file1.txt file2.txt file3.txt

-> File Permissions and Ownership

Types of Permissions-

Read (r): Permission to read the contents of the file.

Write (w): Permission to modify the file.


Execute (x): Permission to execute the file as a program/script.

Permission Levels
Owner: The user who owns the file.
Group: The group that owns the file.
Others: All other users.

Permissions are displayed as a string of 10 characters. The first character indicates the file type
(e.g., - for a regular file, d for a directory), and the next nine characters represent the permissions
for the owner, group, and others in sets of three.

Ex: -rwxr-xr--

' - ': Regular file.


' rwx ': Owner has read, write, and execute permissions.
' r-x ': Group has read and execute permissions.
' r-- ': Others have read permission.

Permissions are represented by octal numbers:

r=4
w=2
x=1

Combine these values to set permissions. For example:

7 (4+2+1) = rwx
6 (4+2) = rw-
5 (4+1) = r-x

Example: Set read, write, and execute permissions for the owner, and read and execute for group
and others on file.txt:

chmod 755 file.txt

chmod: Change file permissions.


Ex- chmod 755 file.txt

chown: Change file owner and group.


Ex- chown user:group file.txt

chgrp: Change group ownership.


Ex- chgrp group file.txt

Grant execute permission to the owner of file.txt:

chmod u+x file.txt

-> Process Management

ps: Display current processes.


Ex- ps aux
ps -ef

Search for a Process with Specific Options.


To search for processes containing java and display only those processes:

ps -ef | grep java

top, htop: Real-time system monitoring.


Ex- htop

kill, pkill: Terminate processes.


Ex- kill -9 PID

nohup: Run a command immune to hangups.


Ex- nohup command &

-> Networking

ifconfig, ip: Configure network interfaces.


Ex- ip a

ping: Check network connectivity.


Ex- ping google.com

netstat, ss: Network statistics.


Ex- ss -tuln

scp: Secure copy files between hosts.


Ex- scp local_file user@remote_host:/path/to/destination

ssh: Securely connect to remote hosts.


Ex- ssh user@remote_host

Intermediate Commands

-> Archiving and Compression

tar: Archive files.


Ex- tar -cvf archive.tar /path/to/directory
tar -xvf archive.tar

gzip, gunzip: Compress and decompress files.


Ex- gzip file.txt
gunzip file.txt.gz

zip, unzip: Compress and decompress ZIP files.


Ex- zip archive.zip file.txt
unzip archive.zip

-> File Searching and Text Processing


find: Search for files in a directory hierarchy.
Ex- find /path/to/search -name "*.txt"

grep: Search text using patterns.


Ex- grep "pattern" file.txt

Use Grep with Pipe

Use grep with the output of another command

ps aux | grep "process_name

awk: Pattern scanning and processing language.


Ex- awk '{print $1}' file.txt

sed: Stream editor for filtering and transforming text.


Ex- sed 's/old/new/g' file.txt

-> System Monitoring and Performance

df: Report filesystem disk space usage.


Ex- df -h

du: Estimate file space usage.


Ex- du -sh /path/to/directory

vmstat: Report virtual memory statistics.


Ex- vmstat 5

iostat: Report CPU and I/O statistics.


Ex- iostat -x 5

You might also like