Author: Ayush Maggo
The most useful commands:
- man command
The first command I want to introduce is a command that will help you to understand all the other commands.
The man command provides a detailed description, Synopsis, Author name, applicable options, one-line description, and other informative elements of the specified command.
$ man pwd
- whoami command
The whoami command allows users to view the currently logged-in user.
ayush@IN2$ whoami
ayush
- id command
The id command prints the real user id (uid) and group id (gid), and various other details related to the account.
ayush@IN2$ id
uid=1000(ayush) gid=1000(ayush) groups=1000(ayush),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),116(netdev)
‘-u’ option is used to find a user’s specific user ID
ayush@IN2$ id -u ayush
1000
- pwd command
The pwd command (Print Working Directory) will help to find the path of the working directory.
ayush@IN2$ pwd
/home/ayush
- ls command
The ls command is used to list the files and directories inside the directory.
ayush@IN2$ ls
linux_demo
If we add a folder name or path, it will print that folder contents.
$ ls /bin
If we add a folder name or path, it will print that folder contents.
- ls -l command
The “-l” option stands for “long format” and provides detailed information about each file and directory in the directory.
$ ls -l
total 0
drwxr-x--- 1 ayush ayush 4096 Jun 3 17:21 ayush
drwxr-xr-x 1 root root 4096 Nov 25 2022 linuxbrew
. . .
- ls -a command
The ls -a command is similar to ls -l command but ls -a command also displays the hidden file.
$ ls -a
. .. ayush linuxbrew
- ls -ltr command
The “-ltr” option combines three different options: “-l” is for “long format”, “-t” is for “sort by time”, “-r” is for “reverse the order of sorting”. When we run the ls -ltr command, it displays the files and directories in long format, sorted by modification time in descending order (newest files first). This is useful when we want to see the most recently modified files at the end of the list.
$ ls -ltr
-rw-r--r-- 1 user group 2095 May 01 10:30 file1.txt
-rw-r--r-- 1 user group 4050 May 15 12:45 file2.txt
drwxr-xr-x 2 user group 8066 May 20 17:10 directory
. . .
- mkdir command
Using the mkdir command, we can create folders.
$ mkdir linux_demo
$ ls
linux_demo
linux_demo is the folder name.
We can create multiple folders using the mkdir command.
$ mkdir bike car
$ ls
bike car
We can also create multiple nested folders by adding the “-p” flag.
$ mkdir -p animals/dog
$ ls
animals
$ cd animals/
$ ls
dog
- rmdir command
The rmdir command deletes a folder. The folder you delete might be empty.
$ rmdir dog
We can also delete multiple folders at once.
$ rmdir bike car
- clear command
This command is used to clear all the previous commands that were run in the current terminal.
$ clear
- history command
This command displays the previously executed command.
$ history
- cd command
The cd command (change directory) will help to change your current directory.
We will move to the bin directory.
$ cd /bin
- . directory and .. directory
The “.” (dot) represents the current directory.
The “..” (dot dot) represents the parent directory of the current directory.
cd . -> The user will remain in the current directory with this command.
cd .. -> This command will move the user up one directory.
$ cd ..
$ cd .
- tree command
The tree command prints the directory structure.
$ tree
.
├── ayush
└── linuxbrew
- Becoming root user
The root is the superuser. root has the ability to make changes in various parts of a Linux system.
The su – command is used to become the root user.
$ su -
“/” represents the root directory of your file system.
$ ls /
bin boot ...
exit keyword is used to exit from the root user.
- sudo
The sudo command is used to run a command with admin privileges known as root access.
$ sudo apt update
- touch command
By using the touch command, we can create an empty file (text file).
$ touch orange
$ ls
orange
“Orange” is the name of the file.
touch orange.txt{1..5} command creates new files of the names orange.txt1, orange.txt2, orange.txt3, orange.txt4, orange.txt5
$ touch orange.txt{1..5}
$ ls
orange.txt1, orange.txt2, orange.txt3, orange.txt4, orange.txt5
- Vim editor
The vim is a very popular file editor. It’s also known as vi improved editor. The vim is the command to open an existing file for editing. If the file does not exist, it will open a new and empty file for editing.
“flower” is the name of the file.
$ vim flower
Press escape and then type “:q” to exit from vim
Press “:“ and type “wq” to save and quit the vim editor.
To exit the vim editor without saving, press “:” and write “q!”.
- cat command
The cat command is a very important command in our day to day activity; it prints a file’s content to the standard output.
$ cat flower
Hello
(Hello is written in example file)
- nano editor
The nano is also an editor.
Run it using nano <filename>
- echo command
The echo command is used to display the text passed in as an argument.
$ echo "Hi"
Hi
We can append the output to a file by using the “>>” operator.
echo <text> >> <filename>
- Using > and >> to redirect output to a particular file
By using the “>” operator, we can redirect the output of one command to a file, if the content is present in that file already then it will remove the old content and only keep the output of the command.
$ ls > a.txt
$ ls
a.txt app cat dog
$ cat a.txt
a.txt
app
cat
dog
We can use the “>>” operator to append to a file, along with the output of the command it will keep the old content also.
- wc command
The wc, short for word count, helps us to count lines, words and bytes of a file.
The “-l” flag gives the number of lines in a file.
$ wc -l <filename.txt>
The “-w” flag counts the number of words in the file.
$ wc -w <filename.txt>
- Redirecting the command output
Using pipe (|) operator, we can redirect the command output to another command as input.
$ ls
bin boot ...
$ ls | wc -l
23
- rm command
The rm command is a very useful command and it is used to remove a file, or directory. The “-r” option is used to remove in a recursive way. The “-f” option is used to remove forcely.
$ rm -rf <folder_name>
rm <file_name> command deletes all the files which have the name starting with <file_name>
- cp command
Using cp command, we copy a file in the Linux shell. In below example, we copy the text of file a.txt into file b.txt
$ cp a.txt b.txt
In the below example, we copy the images directory (and everything inside it)
from the Pictures directory under home to the /tmp/ directory.
$ cp -r ~/Pictures/images /tmp/
In another example, we copy the file image.png from the Pictures directory in my
home directory to the current directory.
$ cp ~/Pictures/image.png .
- mv command
The mv command is used to rename or move a directory or file.
$ mv good.txt bad.txt
The file good.txt is renamed to bad.txt
- cal command
The cal command is used to display a calendar in the shell.
$ cal
- date command
The date command displays the current date.
$ date
- chmod command
It stands for change mode. chmod command is used to change the access permissions of files and directories in Linux systems.
chmod can be used in 2 ways:
- First is using symbolic arguments.
- Second is using numeric arguments.
Symbolic letters are:
- a stands for all
- u stands for user
- g stands for group
- o stands for other
Then type “+” to add a permission or “-” to remove the permission. Then write
one or more permissions symbols (r,w,x).
Some examples are:
- chmod a+r filename # everyone can now read
- chmod u+rw filename
Numeric letters can also be used to give permission. To establish the
permissions for all 3 groups together, use them in sets of three.
Numeric letters are:
- 0 can be used for no permissions
- 1 can be used for execute
- 2 can be used for for write
- 3 can be used for write, execute
- 4 can be used for read
- 5 can be used for read, execute
- 6 can be used for read, write
- 7 can be used for read, write, and execute.
Some examples are:
chmod 777 filename
chmod 000 filename
chmod 655 filename
32. chown command
The owner and the root user can change the owner to another user by using the chown command.
sudo chown <owner> <file>
33. chgrp command
The group ownerships of files and directories can be changed with the chgrp command.
chgrp <group> <filename>
Happy Learning!