learning commands in linux
learning commands in linux
Usage : The ls command is used to list all directories and files in the Linux
terminal. When we run the command basically it displays the contents of the
current directory or folder so that users can view the names of file, directories,
and their information. The ls command use the default option to show the files in
alphabetical order but it can be modified also with different options to display
extra information such as permissions, ownership, and size.
Commands Used:
Þ ls :- Shows all files and folders in the current directory.
Þ ls -l :- Lists with details (permissions, size, dates, etc.)
Þ ls -a :- Includes hidden files (those starting with .).
Output:
1.2. Command Name: cd
Usage : The 'cd' command allows users to change their current working directory
within the file system.
Syntax: cd [directory]
Commands Used:
Þ cd ~/[directory] :- Go to a specific directory.
Þ cd .. :- Go up one level to the parent directory.
Þ cd :-Go to your home directory).
Output:
Usage : The grep command in Linux is a powerful tool used for searching and
manipulating text patterns within files. Its name is derived from the ed (editor)
command g/re/p (globally search for a regular expression and print matching
lines), which reflects its core functionality.
Commands Used:
Þ grep "cute" myfile.txt :- This searches the file “myfile.txt” for lines
containing "cute" and prints those lines.
Þ grep -v "pattern" filename :- This prints out all the lines that do not
matches the pattern in a file “filename”.
Þ grep -c "pattern" filename :- This prints only a count of the lines that
match a pattern.
Þ grep -n "pattern" filename :- Display the matched lines and their line
numbers.
Output:
Usage : The su stands for (substitute user), it is a command in Linux that is used
to switch from one user to another user account within the current shell session.
It is useful for quick switching between the users without changing the existing
environment setup.
Syntax: su [username]
Commands Used:
Þ su username :- Used to switch to another user from the terminal. This
switches to user “username” after entering the password for that user.
Output:
Usage : sudo (Super User DO) command in Linux is generally used as a prefix
for some commands that only superusers are allowed to run. If you prefix any
command with "sudo", it will run that command with elevated privileges or in
other words allow a user with proper permissions to execute a command as
another user, such as the superuser. This is the equivalent of the "run as
administrator" option in Windows. The option of sudo lets us have multiple
administrators.
Commands Used:
Þ sudo -V :- causes sudo to print the version number and exit. If the
invoking user is already root, the -V option will print out a list of the
defaults sudo was compiled with.
Þ sudo -l :- prints out the commands allowed (and forbidden) the user on
the current host.
Output:
Usage : The 'pwd,' which stands for "print working directory." It prints the path
of the working directory, starting from the root.
Commands Used:
Þ pwd -L :- Prints the symbolic path.
Þ pwd -P :- Prints the actual path
Output:
Usage : `mv` stands for "move". The mv command is for renaming and moving
files and directories within a filesystem. Although desktop operating systems
have graphical user interfaces for file operations, calling mv in the terminal,
usually offers a better approach.
Commands Used:
Þ mv oldname.txt newname.txt :- Renames an already existing file.
Þ mv file.txt ~/Downloads/ :- Moves a file to another folder.
Output:
1.8. Command Name: cp
Usage : The cp (copy) command is used for duplicating files and folders quickly.
cp lets you copy items between locations while keeping the original intact. The
cp command requires at least two filenames in its arguments.
Commands Used:
Þ cp file.txt backup.txt :- This creates a new file ‘backup.txt’ that’s a copy of
‘file.txt’.
Þ cp file1.txt file2.txt ~/Backup/:- This makes copies of the mentioned files
into a folder ‘Backup’.
Output:
Syntax: rm filename
Commands Used:
Þ rm myfile.txt :- This deletes ‘myfile.txt’ with no confirmation and no Trash
— it's gone immediately.
Output:
1.10. Command Name: mkdir
Usage : 'mkdir' stands for "make directory," and it helps you organize your
computer stuff by creating folders with just one command. This command can
create multiple directories at once as well as set the permissions for the
directories. It is important to note that the user executing this command must have
enough permission to create a directory in the parent directory, or he/she may
receive a 'permission denied' error.
Commands Used:
Þ mkdir directory :- creates a directory named ‘directory’ in the current
location.
Output:
Output:
Usage : The chmod command is used to change the access mode of a file. The
name is an abbreviation of change mode which states that every file and directory
has a set of permissions that control the permissions like who can read, write or
execute the file. In this the permissions have three categories: read, write, and
execute simultaneously represented by `r`, `w` and `x`. These letters combine
together to form a specific permission for a group of users.
The `chmod` command is used to modify this permission so that it can
grant or restrict access to directories and files. Each permission is represented by
a number:
Permission Value
Read (r) 4
Write (w) 2
Execute (x) 1
Commands Used:
Þ chmod 755 myFile.txt :- This will provide full access for owner,
read/execute for others since:
Owner: read + write + execute (7)
Group: read + execute (5)
Others: read + execute (5)
Output:
Usage : The cat command in Linux is used for various file-related operations,
allowing users to view, concatenate, create, copy, merge, and manipulate file
contents.
Commands Used:
Þ cat myfile.txt :- This prints the entire contents of “myfile.txt” to the
terminal.
Output:
Usage : The chown command, short for "change owner," is a powerful tool that
allows users to change owner of file in Linux. This command is particularly
useful in scenarios where administrators need to grant or revoke access to specific
resources.
Commands Used:
Þ sudo chown anjalo sample.txt :- This command makes anjalo the new
owner of the file sample.txt
Output:
Usage : The echo command in Linux is a built-in command that allows users to
display lines of text or strings that are passed as arguments. It is commonly used
in shell scripts and batch files to output status text to the screen or a file.
Commands Used:
Þ echo "Hello, World" :- echo command prints texts inside the double
quotations.
Output:
Usage : wc stands for word count. It is mainly used for counting purpose. It is
used to find out number of lines, word count, byte and characters count in the
files specified in the file arguments. By default, it displays four-columnar output.
First column shows number of lines present in a file specified, second column
shows number of words present in the file, third column shows number of
characters present in file and fourth column itself is the file name which are given
as argument.
Output:
Usage : The "man" command, short for manual, is a powerful tool in the Linux
operating system that allows users to access detailed information about various
commands, utilities, and system calls. The "man" command provides
comprehensive documentation, helping users understand how to use and
configure different elements of the Linux environment.
Commands Used:
Þ man ls :- This shows the manual page for the ls command.
Output:
1.18. Command Name: history
Usage : The history command in Linux is essential for terminal users, enabling
them to view, search, and manipulate previously executed commands. Mastering
this Linux command allows for efficient commands of commands, automation of
repetitive tasks, and troubleshooting without the need to retype lengthy
commands.
Syntax: history
Commands Used:
Þ history :- history command shows a list of all previously run commands
in our current terminal session (and older ones too, if saved).
Output:
Usage : clear is used to clear the terminal screen. This command first looks for a
terminal type in the environment and after that, it figures out the terminfo
database for how to clear the screen. And this command will ignore any
command-line parameters that may be present. Also, the clear command doesn't
take any argument and it is almost similar to cls command on a number of other
Operating Systems.
Syntax: clear
Commands Used:
Þ clear :- clears the terminal screen.
Output:
Commands Used:
Þ touch notes.txt :- If ‘notes.txt’ doesn't exist, it will be created as an empty
file. If it already exists, its modification time will be updated to the
current time.
Þ touch foldername/myfile.txt :- Creates a file named ‘myfile.txt’ in folder
named ‘foldername’.
Output:
Commands Used:
Þ locate notes.txt :- Finds the path of file ‘notes.txt’. It shows all matching
paths instantly.
Output:
Commands Used:
Þ netstat -t :- Shows TCP connections.
Þ netstat -u :- Shows UDP connections.
Þ netstat -lt :- This command specifically lists the TCP ports that are in a
listening state.
Output:
1.23. Command Name: ss
Usage : 'ss' command stands for "Socket Statistics". It is a potent tool for
inspecting and displaying detailed information about network sockets on a Linux
system. The 'ss' command is an indispensable resource for network
administrators, system administrators, and developers, offering insights into
network connections, routing tables, and more.
Syntax: ss [options]
Commands Used:
Þ ss -t :- Displays TCP sockets.
Þ ss -u :- Displays UDP sockets
Þ ss -s :- Displays summary statistics.
Þ ss -4 :- Displays only IPv4 sockets.
Output:
Output:
Output:
1.27. Command Name: passwd
Usage : The passwd command in Linux provides a straightforward and effective
way to modify user passwords.
Syntax: passwd [options] [username]
Commands Used:
Þ passwd -h :- Displays help related to the `passwd` command.
Output:
Output:
1.29. Command Name: userdel
Usage : 'userdel' command is used to delete a user account and related files. The
'userdel' command allows administrators to delete user accounts from the system.
It is a part of the user management tools that also include commands like 'useradd'
and 'usermod'. The 'userdel' command not only removes the user account but also
takes care of associated files, such as the home directory.
Syntax: sudo userdel [options] username
Commands Used:
Þ sudo userdel -f newuser :- Force removal of the user account, including
home directory and mail spool, even if the user is logged in
Output:
Output:
1.31. Command Name: apt
Usage : apt provides a high-level Command Line Interface (CLI) for the APT
package management system, offering a user-friendly interface intended for
interactive use. It simplifies common tasks like installation, upgrades, and
removal, with better defaults than more specialized tools like apt-get and apt-
cache.
Syntax: apt [...COMMANDS] [...PACKAGES]
Commands Used:
Þ sudo apt autoremove :- Removes unused packages.
Output:
Output:
Usage : The cut command in linux is a command for cutting out the sections from
each line of files and writing the result to standard output. It can be used to cut
parts of a line by byte position, character, and field. The cut command slices a
line and extracts the text. It is necessary to specify an option with a command
otherwise it gives an error. If more than one file name is provided then data from
each file is not preceded by its file name.
Usage : The sort command in Linux is used to sort a file, arranging the records
in a particular order. By default, the sort command sorts file assuming the contents
are ASCII. Using options in the sort command can also be used to sort
numerically.
Usage : The date command in Linux allows the user to display the current date
and time in a variety of formats and set the system date and time. Being a part of
the core utilities in Linux and Unix-like operating systems, it makes it an essential
command for system configuration and scripting. However, by default, the date
displays the current time in the system's configured timezone. It supports
formatting using the special date format specifiers to display the time exactly the
way you need it.
Output:
1.42. Command Name: cal
Usage : cal command is a command in Linux which is used to see the calendar
of a specific month or a whole year. By default, entering cal in the terminal shows
the calendar of the current month, with today's date highlighted. This provides a
quick overview of the month at hand.
Output:
Usage : host command in Linux system is used for DNS (Domain Name System)
lookup operations. In simple words, this command is used to find the IP address
of a particular domain name or if you want to find out the domain name of a
particular IP address the host command becomes handy. You can also find more
specific details of a domain by specifying the corresponding option along with
the domain name.
Usage : The wget command in Linux is used to download files from the web via
HTTP, HTTPS, or FTP.
Commands Used:
Þ wget https://example.com/index.html :- This downloads the file named
index.html from the website example.com. The file will be saved in the
current folder. Basically, it fetches a small web page file from the internet
onto your computer.
Output:
Usage : The 'id' command in Linux is used to display user and group names along
with their numeric IDs (User ID - UID or Group ID - GID) of the current user or
any specified user on the system. This command is particularly useful for system
administrators and users who need to verify user identities and associated
permissions.
Commands Used:
Þ id username :- This shows the same information but for the user called
‘username’.
Output:
Usage : The `ps` command, which stands for "process status," is like a computer
tool that helps you see what's happening inside your Linux computer. Imagine
your computer is doing several things simultaneously, like running different
programs or apps. These are the processes and the `ps` command lets you take a
quick look at them.
Syntax: ps [options]
Commands Used:
Þ ps :- Shows the processes running in the current shell/session.
Output:
Usage : top command is used to show the Linux processes. It provides a dynamic
real-time view of the running system. Usually, this command shows the summary
information of the system and the list of processes or threads which are currently
managed by the Linux Kernel. As soon as you will run this command it will open
an interactive command mode where the top half portion will contain the statistics
of processes and resource usage. And Lower half contains a list of the currently
running processes. Pressing q will simply exit the command mode.
Commands Used:
Þ top :- Shows a real-time view of a system's processes — like a live task
manager in the terminal.
Output:
Commands Used:
Þ kill 1234:- This sends signal 15 (SIGTERM) to process 1234, asking it to
stop politely.
Output:
Usage : The pkill command is used to kill a process by its name instead of its
PID. It’s like kill, but more user-friendly because you don’t need to find the
process ID (PID).
Commands Used:
Þ pkill firefox :- This will stop all processes with the name ‘firefox’.
Þ pkill -u student firefox :- This will kill ‘firefox’ processes started by user
‘student’.
Output:
Usage : The primary contrast between the kill and kill all commands is that the
"kill" ends process cycles dependent on Process ID number (PID), while the kill
orders end running cycles dependent on their names and different attributes.
Normal users can end/kill their cycles(processes), but not those that have a place
with different users, while the root client can end all cycles.
Usage : In Linux, the bg command is a useful tool that helps to manage and move
processes between the foreground and background. It's especially helpful when
multitasking in the terminal by placing a process in the background, enabling
continuing using the terminal for other commands while the process runs quietly
in the background.
Syntax: bg [job_spec]
Commands Used:
Þ sleep 30 :- This starts a command that waits 30 seconds.
Þ bg :- Resumes it in the background, so the terminal stays free.
Output:
Usage : The fg command in Linux is used to bring a background job into the
foreground. It allows you to resume a suspended job or a background process
directly in the terminal window, so you can interact with it.
Syntax: fg [job_spec]
Commands Used:
Þ sleep 100 :- This starts a command that waits 100 seconds.
Þ bg :- Resumes it in the background, so the terminal stays free.
Þ fg :- Brought it back to the foreground so you can interact or stop it.
Output:
Usage : The tar command is used to archive (combine) and compress files or
directories. It stands for “tape archive” and is commonly used to create .tar or
.tar.gz files.
Commands Used:
Þ tar -cvf myfolder.tar myfolder/ :- Creates ‘myfolder.tar’ containing the
contents of ‘myfolder’.
Output:
1.55. Command Name: gzip
Usage : The gzip command is used to compress files in Linux. It reduces the
file size using the .gz (GZIP) compression format. It compresses a single file
(makes it smaller) and replaces the original file with a .gz version which can be
used with gunzip to extract the file later.
Commands Used:
Þ gzip notes.txt :- This compresses notes.txt and creates notes.txt.gz. The
original notes.txt is removed by default.
Þ gunzip notes.txt.gz :- This restores notes.txt and deletes the .gz file.
Output:
Commands Used:
Þ alias ll='ls -la' :- This creates a shortcut for ‘ls -la’. Now typing ‘ll’ will
run ‘ls -la’.
Output:
Usage : The who command is a simple and effective way to display information
about currently logged-in users. By typing who in the terminal, you will receive
a list of usernames, terminal IDs, login times, and originating IP addresses if
applicable. The who command is used to get information about currently
logged-in users on the system. The who command is used to find out the
following information :
Time of last system boot
Current run level of the system
List of logged-in users and more.
Usage : The whoami command shows the current logged-in username — that
is, who you are in the system.
Syntax: whoami
Commands Used:
Þ whoami :- It shows the username of the person currently logged in to the
terminal.
Output:
Usage : The 'w' command in Linux gives us important information about who is
currently using the computer, how much the computer is being used, and what
programs are running. It's a handy tool for people who take care of computer
systems, as it helps them keep an eye on what users are doing, how much of the
computer's power is being used, and how to make everything run smoothly.
Commands Used:
Þ w :- It displays usernames, login times, idle time, and current commands. It
also shows system uptime and load average.
Output:
1.60. Command Name: uptime
Usage : It is used to find out how long the system is active (running). This
command returns set of values that involve, the current time, and the amount of
time system is in running state, number of users currently logged into, and the
load time for the past 1, 5 and 15 minutes respectively.
Syntax: uptime
Commands Used:
Þ w :- It shows how long your computer (Linux system) has been running,
how many people are logged in, and how busy the system is.
Output:
Usage : The free command is used to display the amount of free and used
memory (RAM and swap) on your system. It shows how much memory is used,
free, shared, cached, and available and helps monitor system performance.
Syntax: free
Commands Used:
Þ free :- It shows how much memory (RAM and swap) your Linux system is
using and how much is free.
Output:
1.62. Command Name: xargs
Usage : The xargs command is used to build and execute command lines from
standard input. In simpler words, it takes output from one command and uses it
as arguments for another command. It converts input from a pipe or file into
arguments for a command. It’s useful when commands don’t accept input
directly from pipes.It helps run commands on many files or items efficiently.
Commands Used:
Þ echo "file1.txt file2.txt file3.txt" | xargs -n 1 echo :- Prints each file name
on its own line.
Þ echo "file1.txt file2.txt" | xargs ls -l :- Runs ls -l on the two files and
shows detailed info.
Output:
Commands Used:
Þ crontab -e :- Opens your crontab file in a text editor where you can add
scheduled tasks.
Output:
Usage : The env command shows the environment variables for your current
shell session. Environment variables are settings that affect how processes run,
like your PATH, user info, and more. It displays all environment variables and
their values. It can be used to run a command with a modified environment.
Syntax: env
Commands Used:
Þ env | grep anjalirai :- Runs env to list all environment variables and pipes
(|) that list into grep USER to filter and show only variables containing
"anjalirai".
Output:
1.65. Command Name: printenv
Syntax: printenv
Commands Used:
Þ printenv HOME :- Shows the current user's home directory.
Output:
Usage : The mount command in Linux is a powerful tool used to attach file
systems to the directory tree, making them accessible to the operating system and
users. In simpler terms, it allows you to connect storage devices (like USB drives,
hard disks, or network shares) to specific directories (called mount points) so you
can access the files stored on them.
Commands Used:
Þ sudo mkdir /mnt/mydisk :- Creates a directory to mount onto.
Þ dd if=/dev/zero of=disk.img bs=1M count=10:- Create a disk image file
(a fake "disk").
Þ mkfs.ext4 disk.img:- Formats it with a filesystem.
Þ sudo mount -o loop disk.img /mnt/mydisk :- Mounts the image file using
a loop device.
Þ ls /mnt/mydisk :- Access the mounted "disk".
Þ sudo umount /mnt/mydisk :- When done, unmount.
Output:
Usage : To unmount mounted file systems from their mount points, use the
umount command. Before physically removing a storage device or making
configuration changes, it is essential to unmount the file system. It is a vital
instrument for securely disconnecting network shares or USB drives, among
other types of storage, from the Linux file system. For the purpose of preventing
data loss or corruption, the command makes sure each of the current file
operations has concluded. This command is typically utilized by users to free up
resources and allow the removal or upkeep of devices.
Commands Used:
Þ mkdir ~/folder1 :- Creates a folder.
Þ echo "hello" > ~/folder1/hello.txt :- Adds a file inside folder1.
Þ sudo mount --bind ~/folder1 ~/folder2 :- Formats it with a filesystem.
Þ sudo mount -o loop disk.img /mnt/mydisk :- Mounts folder1 into folder2.
Þ ls ~/folder2 :- Checks folder2.
Þ sudo umount ~/folder2 :- Unmounts folder2.
Output:
Usage : The `journalctl` command is part of the systemd suite of utilities and is
used to query and display log messages from the systemd journal. The systemd
journal is a centralized logging system that collects and stores log data from
various sources, including system services, kernel events, and user applications.
The journalctl command provides a user-friendly interface to access and retrieve
log information, allowing users to effectively monitor system activity and
diagnose issues.
Commands Used:
Þ journalctl -n 5:- Shows the last 5 log entries from the system journal
(recent system messages).
Output:
1.69. Command Name: lsblk
Usage : The lsblk (list block devices) command shows all storage devices
attached to your system — like hard drives, USB drives, and partitions. It lists
storage devices (disks, partitions, etc.) and helps identify device names (like
/dev/sda, /dev/sdb1) but it does not show RAM or CD-ROMs unless specified.
Syntax: lsblk
Commands Used:
Þ lsblk :- It lists all block storage devices on your system — like hard
drives, SSDs, USB drives, and their partitions.
Output:
Usage : The nano command opens a simple text editor in the terminal. It lets you
create or edit text files easily without complex commands.It opens a text editor
in the terminal window. It allows you to write or modify text files. It shows
helpful shortcuts at the bottom (like saving and exiting).
Commands Used:
Þ nano Notes.txt :- It opens the file named Notes.txt in the nano text editor
inside the terminal. If Notes.txt exists, you can edit its content. If it
doesn't exist, nano will create a new empty file called Notes.txt for you to
write in.
Output:
Usage : vim is a powerful text editor used in the terminal. It’s more advanced
than nano and widely used by programmers and sysadmins. It opens a text file
for editing in the terminal. It has two main modes:
Normal mode (for navigation and commands)
Insert mode (for typing text)
Commands Used:
Þ vim Notes.txt :- It opens the file named Notes.txt in the vim text editor
inside the terminal. If Notes.txt exists, you can view and edit it. If it
doesn't exist, vim creates a new empty file named Notes.txt for you to
edit.
Output:
1.72. Command Name: less
Usage : At its core, the 'less' command operates as a powerful pager program,
optimized for quick and resource-efficient viewing of text data. 'less' provides a
pragmatic solution for exploring and searching text without the overhead of file
editors like vi or nano.Insert mode (for typing text)
Commands Used:
Þ head -n 10 /var/log/syslog | less :- It shows you the top 10 lines of the system log in
a scrollable viewer, so you can easily read or exit.
Output:
Usage : tee command reads the standard input and writes it to both the standard
output and one or more files. The command is named after the T-splitter used in
plumbing. It basically breaks the output of a program so that it can be both
displayed and saved in a file. It does both the tasks simultaneously, copies the
result into the specified files or variables and also display the result.
Commands Used:
Þ ls -l | tee files.txt :- Lists files in long format, showing the output on the
screen and saving a copy to the file files.txt at the same time.
Output:
Usage : scp (secure copy) is used to copy files between computers securely over
a network using SSH. It copies files or directories from your computer to a remote
machine, or vice versa. It encrypts data during transfer for security.
Commands Used:
Þ scp test.txt user@example.com:/home/user/ :- It securely copies the
file ‘test.txt’ from your local computer to the directory /home/user/ on the
remote computer identified by example.com, logging in as the user named
‘user’.
Output:
Usage : The rsync command is used to efficiently copy and synchronize files and
directories between locations, either on the same machine or over a network. It
only transfers the differences between source and destination, which makes it
faster than a simple copy for repeated transfers.
Syntax: rsync [options] source destination
Commands Used:
Þ rsync myFile.txt /home/anjalirai/Documents/ :- This copies
‘myFile.txt’ to the backup folder ‘Documents’.
Output:
Usage : The ssh command lets you securely connect to a remote computer over
a network.
Commands Used:
Þ ssh [options] user@hostname :- This opens a terminal session on the
remote machine where you can run commands just like you’re sitting in
front of it.
Output: