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

linux_cmd[1]

The document provides a comprehensive overview of various Linux commands categorized into file and directory operations, file permissions, process management, system information, networking, IO redirection, environment variables, user management, and shortcuts. Each command is accompanied by a description, options, and examples for better understanding. This serves as a useful reference for users looking to navigate and utilize Linux effectively.

Uploaded by

deepanshi.lskdm
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)
3 views

linux_cmd[1]

The document provides a comprehensive overview of various Linux commands categorized into file and directory operations, file permissions, process management, system information, networking, IO redirection, environment variables, user management, and shortcuts. Each command is accompanied by a description, options, and examples for better understanding. This serves as a useful reference for users looking to navigate and utilize Linux effectively.

Uploaded by

deepanshi.lskdm
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/ 14

1.

File and Directory Operations Commands

File and directory operations are fundamental in working with the Linux operating system. Here are some
commonly used File and Directory Operations commands:

Command Description Options Examples

ls List files and • -l: Long format • ls -l


directories. listing. displays files and directories
with detailed information.
• -a: Include hidden
files hidden ones • ls -a
shows all files and directories,
• -h: Human-
including
readable file sizes.
• ls -lh
displays file sizes in a human-
readable format.

cd Change directory. • cd /path/to/directory


changes the current directory
to the specified path.

pwd Print current working • pwd


directory. displays the current working
directory.

mkdir Create a new • mkdir my_directory


directory. creates a new directory named
“my_directory”.

rm Remove files and • -r: Remove • rm file.txt


directories. directories deletes the file named “file.txt”.
recursively.
• rm -r my_directory
• -f: Force removal deletes the directory
without “my_directory” and its
confirmation. contents.

• rm -f file.txt
forcefully deletes the file
“file.txt” without confirmation.

cp Copy files and • -r: Copy directories • cp -r directory destination


directories. recursively. copies the directory “directory”
and its contents to the
specified destination.

• cp file.txt destination
copies the file “file.txt” to the
specified destination.
mv Move/rename files • mv file.txt new_name.txt
and directories. renames the file “file.txt” to
“new_name.txt”.

• mv file.txt directory
moves the file “file.txt” to the
specified directory.

touch Create an empty file • touch file.txt


or update file creates an empty file named
timestamps. “file.txt”.

cat View the contents of • cat file.txt


a file. displays the contents of the file
“file.txt”.

head Display the first few • -n: Specify the • head file.txt
lines of a file. number of lines to shows the first 10 lines of the
display. file “file.txt”.

• head -n 5 file.txt
displays the first 5 lines of the
file “file.txt”.

tail Display the last few • -n: Specify the • tail file.txt
lines of a file. number of lines to shows the last 10 lines of the
display. file “file.txt”.

• tail -n 5 file.txt
displays the last 5 lines of the
file “file.txt”.

ln Create links between • -s: Create symbolic • ln -s source_file link_name


files. (soft) links. creates a symbolic link named
“link_name” pointing to
“source_file”.

find Search for files and • -name: Search by • find /path/to/search -name
directories. filename. “*.txt”
searches for all files with the
• -type: Search by
extension “.txt” in the specified
file type.
directory.
2. File Permission Commands

File permissions on Linux and Unix systems control access to files and directories. There are three basic
permissions: read, write, and execute. Each permission can be granted or denied to three different
categories of users: the owner of the file, the members of the file’s group, and everyone else.

Here are some file permission commands:

Command Description Options Examples

chmod Change file • u: User/owner • chmod u+rwx file.txt


permissions. permissions. grants read, write, and execute
permissions to the owner of the file.
• g: Group
permissions.

• o: Other
permissions.

• +: Add
permissions.

• –: Remove
permissions.

• =: Set
permissions
explicitly.

chown Change file • chown user file.txt


ownership. changes the owner of “file.txt” to the
specified user.

chgrp Change group • chgrp group file.txt


ownership. changes the group ownership of
“file.txt” to the specified group.

umask Set default file • umask 022


permissions. sets the default file permissions to
read and write for the owner, and
read-only for group and others.
3. File Compression and Archiving Commands

Here are some file compression and archiving commands in Linux:

Commands Description Options Examples

tar Create or extract • -c: Create a new • tar -czvf archive.tar.gz files/
archive files. archive. creates a compressed tar archive
named “archive.tar.gz” containing
• -x: Extract files
the files in the “files/” directory.
from an archive.

• -f: Specify the


archive file name.

• -v: Verbose mode.

• -z: Compress the


archive with gzip.

• -j: Compress the


archive with
bzip2.

gzip Compress files. • -d: Decompress • gzip file.txt


files. compresses the file “file.txt” and
renames it as “file.txt.gz”.

zip Create • -r: Recursively • zip archive.zip file1.txt file2.txt


compressed zip include creates a zip archive named
archives. directories. “archive.zip” containing “file1.txt”
and “file2.txt”.
4. Process Management Commands

In Linux, process management commands allow you to monitor and control running processes on the
system. Here are some commonly used process management commands:

Commands Description Options Examples

ps Display running processes. • -aux: Show all • ps aux


processes. shows all running
processes with detailed
information.

top Monitor system processes • top


in real-time. displays a dynamic view
of system processes and
their resource usage.

kill Terminate a process. • -9: Forcefully kill a • kill PID


process. terminates the process
with the specified
process ID.

pkill Terminate processes • pkill process_name


based on their name. terminates all processes
with the specified name.

pgrep List processes based on • pgrep process_name


their name. lists all processes with
the specified name.

grep used to search for specific • -i: Ignore case • grep -i “hello” file.txt
patterns or regular distinctions while
• grep -v “error” file.txt
expressions in text files or searching.
streams and display • grep -r “pattern”
• -v: Invert the
matching lines. directory/
match, displaying
non-matching lines. • grep -l “keyword”
file.txt
• -r or -R: Recursively
search directories • grep -n “pattern” file.txt
for matching In these examples we
patterns. are extracting our
desirec output from
• -l: Print only the
filename (file.txt)
names of files
containing matches.
• -n: Display line
numbers alongside
matching lines.

• -w: Match whole


words only, rather
than partial
matches.

• -c: Count the


number of
matching lines
instead of
displaying them.

• -e: Specify multiple


patterns to search
for.

• -A: Display lines


after the matching
line.

• -B: Display lines


before the
matching line.

• -C: Display lines


both before and
after the matching
line.
5. System Information Commands

In Linux, there are several commands available to gather system information. Here are some commonly
used system information commands:

sudCommand Description Options Examples

uname Print system • -a: All system • uname -a


information. information. displays all system
information.

whoami Display current • whoami


username. shows the current username.

df Show disk space • -h: Human- • df -h


usage. readable sizes. displays disk space usage in a
human-readable format.

du Estimate file and • -h: Human- • du -sh directory/


directory sizes. readable sizes. provides the total size of the
specified directory.
• -s: Display total
size only.

free Display memory • -h: Human- • free -h


usage information. readable sizes. displays memory usage in a
human-readable format.

uptime Show system uptime. • uptime


shows the current system
uptime.

lscpu Display CPU • lscpu


information. provides detailed CPU
information.

lspci List PCI devices. • lspci


List PCI devices.

lsusb List USB devices. • lsusb


lists all connected USB
devices.
6. Networking Commands

In Linux, there are several networking commands available to manage and troubleshoot network
connections. Here are some commonly used networking commands:

Command Description Examples

ifconfig Display network interface • ifconfig


information. shows the details of all network interfaces.

ping Send ICMP echo requests to • ping google.com


a host. sends ICMP echo requests to “google.com” to check
connectivity.

netstat Display network connections • netstat -tuln


and statistics. shows all listening TCP and UDP connections.

ss Display network socket • ss -tuln


information. shows all listening TCP and UDP connections.

ssh Securely connect to a • ssh user@hostname


remote server. initiates an SSH connection to the specified
hostname.

scp Securely copy files between • scp file.txt user@hostname:/path/to/destination


hosts. securely copies “file.txt” to the specified remote
host.

wget Download files from the • wget http://example.com/file.txt


web. downloads “file.txt” from the specified URL.

curl Transfer data to or from a • curl http://example.com


server. retrieves the content of a webpage from the
specified URL.
7. IO Redirection Commands

In Linux, IO (Input/Output) redirection commands are used to redirect the standard input, output, and
error streams of commands and processes. Here are some commonly used IO redirection commands:

Command Description

cmd < file Input of cmd is taken from file.

cmd > file Standard output (stdout) of cmd is redirected to file.

cmd 2> file Error output (stderr) of cmd is redirected to file.

cmd 2>&1 stderr is redirected to the same place as stdout.

cmd1 <(cmd2) Output of cmd2 is used as the input file for cmd1.

cmd > /dev/null Discards the stdout of cmd by sending it to the null device.

cmd &> file Every output of cmd is redirected to file.

cmd 1>&2 stdout is redirected to the same place as stderr.

cmd >> file Appends the stdout of cmd to file.

8. Environment Variable Commands

In Linux, environment variables are used to store configuration settings, system information, and other
variables that can be accessed by processes and shell scripts. Here are some commonly used environment
variable commands:

Command Description

export VARIABLE_NAME=value Sets the value of an environment variable.

echo $VARIABLE_NAME Displays the value of a specific environment variable.

env Lists all environment variables currently set in the system.

unset VARIABLE_NAME Unsets or removes an environment variable.

export -p Shows a list of all currently exported environment variables.

env VAR1=value COMMAND Sets the value of an environment variable for a specific command.

printenv Displays the values of all environment variables.


9. User Management Commands

In Linux, user management commands allow you to create, modify, and manage user accounts on the
system. Here are some commonly used user management commands:

Command Description

who Show who is currently logged in.

sudo adduser username Create a new user account on the system with the specified username.

finger Display information about all the users currently logged into the system,
including their usernames, login time, and terminal.

sudo deluser USER Remove the specified user from the specified group.
GROUPNAME

last Show the recent login history of users.

finger username Provide information about the specified user, including their username,
real name, terminal, idle time, and login time.

sudo userdel -r username Delete the specified user account from the system, including their home
directory and associated files. The -r option ensures the removal of the
user’s files.

sudo passwd -l username Lock the password of the specified user account, preventing the user from
logging in.

su – username Switch to another user account with the user’s environment.

sudo usermod -a -G Add an existing user to the specified group. The user is added to the
GROUPNAME USERNAME group without removing them from their current groups.
10. Shortcuts Commands

There are many shortcuts commands in Linux that can help you be more productive. Here are a few of the
most common ones:

10.1: Bash Shortcuts Commands:

Navigation Description Editing Description History Description

Ctrl + A Move to the Ctrl + Cut/delete from the cursor Ctrl + R Search command
beginning of the U position to the beginning of history (reverse
line. the line. search).

Ctrl + E Move to the end Ctrl + K Cut/delete from the cursor Ctrl + Escape from history
of the line. position to the end of the G search mode.
line.

Ctrl + B Move back one Ctrl + Cut/delete the word before Ctrl + P Go to the previous
character. W the cursor. command in history.

Ctrl + F Move forward one Ctrl + Y Paste the last cut text. Ctrl + Go to the next
character. N command in history.

Alt + B Move back one Ctrl + L Clear the screen. Ctrl + C Terminate the
word current command.

Alt + F Move forward one


word.
10.2: Nano Shortcuts Commands:

File Description Navigation Description Editing Description Search Description


Operations and
Replace

Ctrl + O Save the Ctrl + Y Scroll up Ctrl + Cut/delete Ctrl + W Search for a
file. one page. K from the string in the
cursor position text.
to the end of
the line.

Ctrl + X Exit Nano Ctrl + V Scroll down Ctrl + Uncut/restore Alt + W Search and
(prompt to one page. U the last cut replace a
save if text. string in the
modified). text.

Ctrl + R Read a file Alt + \ Go to a Ctrl + Mark a block Alt + R Repeat the
into the specific line 6 of text for last search.
current number. copying or
buffer. cutting.

Ctrl + J Justify the Alt + , Go to the Ctrl + Cut/delete the


current beginning K marked block
paragraph. of the of text.
current line.

Alt + . Go to the Alt + 6 Copy the


end of the marked block
current line. of text.
10.3: VI Shortcuts Commands:

Command Description

cw Change the current word. Deletes from the cursor position to the end of the current word
and switches to insert mode.

dd Delete the current line.

x Delete the character under the cursor.

R Enter replace mode. Overwrites characters starting from the cursor position until you press
the Escape key.

o Insert a new line below the current line and switch to insert mode.

u Undo the last change.

s Substitute the character under the cursor and switch to insert mode.

dw Delete from the cursor position to the beginning of the next word.

D Delete from the cursor position to the end of the line.

4dw Delete the next four words from the cursor position.

A Switch to insert mode at the end of the current line.

S Delete the current line and switch to insert mode.

r Replace the character under the cursor with a new character entered from the keyboard.

i Switch to insert mode before the cursor.

3dd Delete the current line and the two lines below it.

ESC Exit from insert or command-line mode and return to command mode.

U Restore the current line to its original state before any changes were made.

~ Switch the case of the character under the cursor.

a Switch to insert mode after the cursor.

C Delete from the cursor position to the end of the line and switch to insert mode.
10.4: Vim Shortcuts Commands:

Normal Description Command Description Visual Description


Mode Mode Mode

i Enter insert mode at :w Save the file. v Enter visual


the current cursor mode to select
position. text.

x Delete the character :q Quit Vim. y Copy the


under the cursor. selected text.

dd Delete the current :q! Quit Vim without d Delete the


line. saving changes. selected text.

yy Copy the current line. :wq Save and quit Vim. p Paste the copied
or deleted text.
or

:x:x

p Paste the copied or :s/old/new/g Replace all


deleted text below the occurrences of “old”
current line. with “new” in the file.

u Undo the last change. :set nu Display line numbers.

or

:set number

Ctrl + R Redo the last undo.

You might also like