0% found this document useful (0 votes)
19 views18 pages

linux commands

Uploaded by

shahbharat2609
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
19 views18 pages

linux commands

Uploaded by

shahbharat2609
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

LAB ASSIGNMENT 1

Subject: Linux Operating System

Submitted To: Mrs. Jasleen Kaur Bains


Submitted By: Bharat Shah
Roll No.: 61
Class: MCA-I (E)
Index
S.No. Linux Commands Page No.

1. sudo command
2. pwd command
3. cd command
4. ls command
5. cat command
6. cp command
7. mv command
8. mkdir command
9. rmdir command
10. rm command
11. touch command
12. locate command
13. find command
14. grep command
15. head command
16. tail command
17. diff, comm, and cmp commands
18. chmod command
19. chown command
20. man command
21. echo command
Linux Command

A Linux command is a program or utility that runs on the CLI (command-line


interface) - a console that interacts with the system via texts and processes. It’s
similar to the Command Prompt application in Windows.
Linux commands are executed on Terminal by pressing Enter at the end of the
line. We can run commands to perform various tasks, from package installation
to user management and file manipulation.

The 40 Most Commonly Used Linux Commands

1. sudo command - Command to escalate privileges in Linux.

sudo is one of the most popular basic Linux commands that lets us to
perform tasks that require administrative or root permissions.

2. pwd command - Print working directory command in Linux

pwd command to find the path of we current working directory. Simply


entering pwd will return the full current path – a path of all the directories
that starts with a forward slash (/).

The pwd command uses the following syntax:


pwd [option]
It has two acceptable options:
 -L or –logical prints environment variable content, including symbolic
links.
 -P or –physical prints the actual path of the current directory.

3. cd command - Linux command to navigate through directories

To navigate through the Linux files and directories, use the cd command.
Running this command without an option will take us to the home folder.
We need to keep in mind that only users with sudo privileges can execute it.
If we want to switch to a completely new directory, for example,
/home/username/Movies, we have to enter cd followed by the directory’s
absolute path:
cd /home/username/Movies
Here are some shortcuts to help us navigate:
 cd ~[username] goes to another user’s home directory.
 cd .. moves one directory up.
 cd- moves to our previous directory.

4. ls command - The most frequently used command in Linux to list directories

The ls command lists files and directories within a system. Running it


without a flag or parameter will show the current working directory’s
content.

To see other directories’ content, type ls followed by the desired path. For
example, to view files in the Documents folder, enter:
ls /home/username/Documents
5. cat command - Display file contents on the terminal

Concatenate, or cat, is one of the most frequently used Linux commands. It


lists, combines, and writes file content to the standard output. To run the cat
command, type cat followed by the file name and its extension. For instance:
cat filename.txt.
Here are other ways to use the cat command:
 cat > filename.txt creates a new file and Enter Ctrl+D to save the file.
 cat filename1.txt filename2.txt > filename3.txt merges filename1.txt
and filename2.txt and stores the output in filename3.txt.
 tac filename.txt displays content in reverse order.
6. cp command - Similar usage as mv but for copying files in Linux

Use the cp command to copy files or directories and their content. Take a
look at the following use cases.
To copy one file from the current directory to another, enter cp followed by
the file name and the destination directory. For example:
cp filename.txt /home/username/Documents
To copy files to a directory, enter the file names followed by the destination
directory:
cp filename1.txt filename2.txt filename3.txt /home/username/Documents
To copy the content of a file to a new file in the same directory, enter cp
followed by the source file and the destination file:
cp filename1.txt filename2.txt
To copy an entire directory, pass the -R flag before typing the source
directory, followed by the destination directory:
cp -R /home/username/Documents /home/username/Documents_backup
7. mv command - Move or rename files in Linux
The primary use of the mv command is to move and rename files and
directories. Additionally, it doesn’t produce an output upon execution.
Simply type mv followed by the filename and the destination directory. For
example, we want to move filename.txt to the /home/username/Documents
directory:
mv filename.txt /home/username/Documents.
we can also use the mv command to rename a file:
mv old_filename.txt new_filename.txt
8. mkdir command - Command used to create directories in Linux
Use the mkdir command to create one or multiple directories at once and set
permissions for each of them.
Here’s the basic syntax:
mkdir [option] directory_name

The mkdir command accepts many options, such as:


 -p or –parents create a directory between two existing folders. For
example, mkdir -p Music/2020/Songs will make the new “2020”
directory.
 -m sets the file permissions. For instance, to create a directory with full
read, write, and execute permissions for all users, enter mkdir -m777
directory_name.
 -v prints a message for each created directory.

9. rmdir command - Permanently delete an empty directory


To permanently delete an empty directory, use the rmdir command. Remember
that the user running this command should have sudo privileges in the parent
directory.
For example, we want to remove an empty subdirectory named personal1 and
its main folder mydir:
rmdir -p mydir/personal1

10. rm command - Delete files within a directory

The rm command is used to delete files within a directory. Make sure that the
user performing this command has write permissions.
Remember the directory’s location as this will remove the file(s) and we can’t
undo it.
Here’s the general syntax:
rm filename

To remove multiple files, enter the following command:


rm filename1 filename2 filename3
Here are some acceptable options we can add:
 -i prompts system confirmation before deleting a file.
 -f allows the system to remove without a confirmation.
 -r deletes files and directories recursively

11. touch command - Create blank/empty files

The touch command allows we to create an empty file or generate and modify a
timestamp in the Linux command line.
For example, enter the following command to create an HTML file named Web
in the Documents directory:
touch /home/username/Documents/Web.html

12. locate command - Find a file in the database system

The locate command can find a file in the database system.


Moreover, adding the -i argument will turn off case sensitivity, so we can search
for a file even if we don’t remember its exact name.
To look for content that contains two or more words, use an asterisk (*). For
example:
locate -i school*not
The command will search for files that contain the words school and note,
whether they use uppercase or lowercase letters.
13. find command - Search for files within a specific directory
Use the find command to search for files within a specific directory and
perform subsequent operations. Here’s the general syntax:
find [option] [path] [expression]
For example, we want to look for a file called notes.txt within the home
directory and its subfolders:
find /home -name notes.txt
Here are other variations when using find:
 find -name filename.txt to find files in the current directory.
 find ./ -type d -name directoryname to look for directories.

14. grep command - Search for a string within an output


Another basic Linux command on the list is grep or global regular expression
print. It lets we find a word by searching through all the texts in a specific file.
Once the grep command finds a match, it prints all lines that contain the
specific pattern. This command helps filter through large log files.
For example, we want to search for the word blue in the notepad.txt file:
grep blue notepad.txt
The command’s output will display lines that contain blue.

15. head command - Return the specified number of lines from the top
The head command allows we to view the first ten lines of a text. Adding an
option lets we change the number of lines shown. The head command is also
used to output piped data to the CLI.
Here’s the general syntax:
head [option] [file]
For instance, we want to view the first ten lines of note.txt, located in the
current directory:
head note.txt
Below are some options we can add:
 -n or –lines print the first customized number of lines. For example,
enter head -n 5 filename.txt to show the first five lines of filename.txt.
 -c or –bytes prints the first customized number of bytes of each file.
 -q or –quiet will not print headers specifying the file name.

16. tail command - Return the specified number of lines from the bottom
The tail command displays the last ten lines of a file. It allows users to check
whether a file has new data or to read error messages.
Here’s the general format:
tail [option] [file]
For example, we want to show the last ten lines of the file3.txt file:
tail file3.txt

17. diff, comm, and cmp commands -


diff - Find the difference between two files
comm - Combines the functionality of diff and cmp
cmp - Allows you to check if two files are identical
Short for difference, the diff command compares two contents of a file line by
line. After analyzing them, it will display the parts that do not match.
Programmers often use the diff command to alter a program instead of rewriting
the entire source code.
Here’s the general format:
diff [option] file1 file2
For example, we want to compare two text files – file3.txt and file4.txt:
diff file3.txt file4.txt
Here are some acceptable options to add:
 -c displays the difference between two files in a context form.
 -u displays the output without redundant information.
 -i makes the diff command case insensitive.
18. chmod command - Command to change file permissions
chmod is a common command that modifies a file or directory’s read, write, and
execute permissions. In Linux, each file is associated with three user classes
– owner, group member, and others.
Here’s the basic syntax:
chmod [option] [permission] [file_name]
For example, the owner is currently the only one with full permissions to
change note.txt. To allow group members and others to read, write, and execute
the file, change it to the -rwxrwxrwx permission type, whose numeric value
is 777:
chmod 777 note.txt

This command supports many options, including:


 -c or –changes displays information when a change is made.
 -f or –silent suppresses the error messages.
 -v or –verbose displays a diagnostic for each processed file.
19. chown command - Command for granting ownership of files or folders
The chown command lets we change the ownership of a file, directory, or
symbolic link to a specified username.
Here’s the basic format:
chown [option] owner[:group] file(s)
For example, we want to make linuxuser2 the owner of filename.txt:
chown linuxuser2 filename.txt

20. man command - Access manual pages for all Linux commands
The man command provides a user manual of any commands or utilities we can
run in Terminal, including the name, description, and options.
It consists of nine sections:
 Executable programs or shell commands
 System calls
 Library calls
 Games
 Special files
 File formats and conventions
 System administration commands
 Kernel routines
 Miscellaneous
To display the complete manual, enter:
man [command_name]
For example, we want to access the manual for the ls command:
man ls
Enter this command if we want to specify the displayed section:
man [option] [section_number] [command_name]
For instance, we want to see section 2 of the ls command manual:
man 2 ls
21. echo command - Print any text that follows the command
The echo command is a built-in utility that displays a line of text or string using
the standard output. Here’s the basic syntax:
echo [option] [string]
For example, we can display the text Linux Basic Commands by entering:
echo “Linux Basic Commands”.

This command supports many options, such as:


 -n displays the output without the trailing newline.
 -e enables the interpretation of the following backslash escapes:
 \a plays sound alert.
 \b removes spaces in between a text.
 \c produces no further output.
 -E displays the default option and disables the interpretation of backslash
escapes.

You might also like