You are on page 1of 116

Basic Linux Commands

Tip#1
• Command completion is a helpful feature that helps you quickly complete the names
of programs, files, or folders that you want to use in a terminal or command prompt.
• Here's an example: Let's say you want to navigate to a folder named "Documents"
using a terminal. Instead of typing the whole word "Documents," you can simply type
"Doc" and then press the TAB key on your keyboard. The shell will automatically
complete the word "Documents" for you, so you don't have to type the whole thing.
• If there are multiple possible completions for the partial word you typed, the shell will
display all the possible options, and you can choose the one you want by typing more
letters and then pressing TAB again.
• It's important to note that shell commands are case sensitive, which means
that uppercase and lowercase letters are treated as distinct characters. For
example, "cd" and "Cd" are not the same command and will produce
different results.
Tip#2
• Repeating the last command is a handy feature that saves you time by letting
you quickly reuse a command you've recently typed in a terminal or command
prompt.
• Here's an example: Let's say you just typed a long command to search for a file
in a specific directory. Later on, you realize you need to run the same command
again. Instead of typing the whole command all over again, you can simply
press the UP arrow key on your keyboard. The shell will retrieve the last
command you typed and display it on the input line, so you can review it and
press Enter to run it again.
• Note that not all commands can be run together in this way. Some
commands may depend on the output of previous commands, and running
them out of order could cause unexpected behavior. Make sure you
understand the dependencies between your commands before running
them in a list.
Tip#3
• Reverse-incremental search is a feature that allows you to search for previously executed
commands in your terminal's command history in reverse order (i.e., from most recent to least
recent).
• Here's an example: Let's say you're trying to remember a command that you recently executed, but
you can't quite recall all the details. Instead of scrolling through your entire command history, you
can use the reverse-incremental search feature. To activate this feature, you need to press the Ctrl
key and the "r" key at the same time. This will display a search prompt where you can start typing a
keyword or phrase that you remember from the command you're looking for.
• As you type, the terminal will display the most recent matching command in your history that
matches your search criteria. You can keep typing to refine your search or press Ctrl-r again to cycle
through older matching commands.
Tip#4
• Running a list of commands is a way to execute multiple commands in a
specific order in a terminal or command prompt.
• Here's an example: Let's say you want to run three commands in a specific
order: first, change to a specific directory, second, list the files in that
directory, and third, run a Python script. Instead of executing each
command separately, you can run them all in a single line by typing each
command in the order you want them to run, separating each command
from the next with a semicolon (;). Here's how it would look:
• Here's how it would look:

• In this example, the semicolon (;) separates each command, telling the
terminal to run the first command, then the second, and then the third.
This way, you can save time and effort by running multiple commands
without having to type them all in separately.
Tip#5
• "!!" is a command that allows you to quickly repeat the last command you executed in a
terminal or command prompt.
• Here's an example: Let's say you just executed a long and complex command that took a
lot of time to type. Later on, you realize you need to run the same command again. Instead
of typing the whole command all over again, you can simply type "!!" (without the quotes)
and press Enter. The shell will retrieve the last command you executed and run it again.
• This feature is especially useful when you need to repeat a command quickly, or when you
need to run a command with slight modifications. For example, you could use "!!" to
quickly run a command with sudo privileges by typing "sudo !!".
• Sudo (short for "superuser do") is a command in Unix-based operating systems that
allows a user with administrative privileges to run commands as a different user,
typically the root user. The root user has full access to all files and system resources on
the computer, and can perform tasks that are not normally allowed for regular users.
• For example, let's say you want to install a new package on your Linux system using
the command line. If you try to install the package without sudo privileges, you may
get an error message indicating that you don't have permission to perform the
operation. However, if you run the same command with sudo privileges, you should be
able to install the package successfully.
Tip#5
• Wildcard characters such as "*" and "?" are special characters that can be
used in command line interfaces to match multiple files or characters in a
pattern.
• Here's an example: Let's say you have a directory with many different
files, some of which have the ".txt" file extension. If you want to see a list
of only the files with this extension, you can use the wildcard character
"*" with the "ls" command,
• Like this:

• In this example, the "*" character is used to match any characters that come before the
".txt" extension, so the command will list all files with the ".txt" extension in the current
directory.
• Similarly, the "?" character can be used to match a single character in a filename. For
example, the command "ls file?.txt" will match filenames like "file1.txt", "file2.txt",
"fileA.txt", but not "file10.txt" or "file.txt".
Tip#7
• The history feature of the command line keeps track of all the commands
you have previously typed. This means that you can easily reuse
commands without having to type them out again.
• For example, let's say you used the "ls" command to list the contents of a
directory earlier in your session. Later on, you need to use the same
command again. Instead of typing out "ls" again, you can simply use the
up arrow key on your keyboard to scroll through your command history
until you find the "ls" command, and then press Enter to run it again.
• Additionally, you can use the "!n" command (where "n" is the number of
the command you want to run) to run a specific command from your
history. For example, if you want to run the 7th command in your history,
you would type "!7" and press Enter.
• The HISTSIZE variable determines how many commands are stored in
your command history. You can change this value to increase or decrease
the number of commands that are saved.
Tip#8
• Aliases are a way to create custom shortcuts for commands that you use frequently. This means
that instead of typing out the full command every time, you can use the shorter alias instead.
• For example, let's say you use the "ls" command often to list the files in a directory, and you
also prefer to see the output in a long format with additional details. Rather than typing out "ls
-l" every time, you can create an alias that combines the "ls" command with the "-l" option. To
do this, you would type the following command:
• alias ls='ls -l'
• This creates an alias called "ls" that is equivalent to the "ls -l" command. Now, whenever you
type "ls" in the command line, it will automatically be replaced with "ls -l".
• You can create aliases for any command that you use frequently, and you
can name them whatever you like. To see a list of your current aliases, you
can use the "alias" command without any arguments. If you want to
remove an alias, you can use the "unalias" command followed by the
name of the alias you want to remove.
A command can be:
• In the command line, there are three types of commands that can be used:
1. Built-in Shell Commands
2. Executable Shell Files known as shell scripts
3. Source compiled, object code files
1. Built-in Shell Commands
• These are commands that are built into the shell or terminal you are using,
and are part of the software itself.
• They are usually simple commands that perform basic operations, such as
changing directories or listing files.
• Examples of built-in shell commands include "cd" to change directory,
"ls" to list files, and "echo" to display text.
2. Executable shell files known as shell
scripts:
• These are files that contain a series of shell commands that can be
executed in sequence.
• Shell scripts are usually used to automate repetitive tasks or to run a
series of commands in a specific order.
• To run a shell script, you need to make sure it has execute permission and
then enter its name in the command line.
• This shell script starts with a "shebang" line, which tells the shell what interpreter to use to execute
the script (in this case, bash). It then includes some comments (lines that start with a "#" symbol) to
provide information about what the script does.
• The script then uses the "echo" command to display some messages to the user. The first message
welcomes the user to the script, the second message displays the current date using the "date"
command, and the third message displays the current working directory using the "pwd" command.
• To run this script, you would need to save it to a file (e.g. "myscript.sh"), make it executable
using the command "chmod +x myscript.sh", and then run it by entering its name in the
command line (e.g. "./myscript.sh"). When executed, the script would display the messages
and information specified in its code
3. Source compiled, object code files:
• These are programs that have been compiled from source code into object
code.
• They are usually more complex than built-in shell commands or shell
scripts, and may be specific to a particular operating system or hardware
platform.
• To run these programs, you need to enter the name of the executable file
in the command line.
Example(Object Code File):

• This is a simple C program that prints the message "Hello, world!" to the
console using the printf function. To create an object code file from this
program, you would need to compile it using a C compiler such as gcc.
• Assuming that the above code is saved to a file named hello.c, you could
compile it to an object code file like this:

• This command tells gcc to compile the hello.c file and output the resulting
object code to a file named hello.o.
•Once you have an object code file, you can link it with other object code files to create an executable program. For example,
you could link the hello.o file with the standard C library to create an executable program like this:

•This command tells gcc to link the hello.o file with the standard C library and output the resulting executable program to a
file named hello.

•When you run the hello program, it will display the message "Hello, world!" to the console, just as the original C program
did.
Commands have ‘3 Parts’ :
1. pwd Command
• stands for <print working directory>
• Prints in which directory we are currently.
2. cd Command
• Has a lot of variations
a) Used for navigation: to enter a specific folder in the current directory.
Note: if the folders name starts with a capital we should write it the same
when using the “cd” command.
Example:
1. cd Desktop: We enter the folder “Desktop” which is present in our
current directory.
2. pwd: We want to check if we entered the folder “Desktop”.
3. ls: lists the contents of the folder (here the folder is empty).
cd Command (2)
b) cd .. Used to go back to the previous directory
c) cd / Used to go to the root directory which contains a list of things one of
them is the home directory that we were in.
Note: We should leave one space between cd and .. , also between cd and /.
3. ls Command
• gives a listing of everything in the current directory
• such as files and directories
• Files with * mean that they are executable.
12. ls –l Command
• Lists all files that are not hidden.
• Gives information about each file.
• Such as who is the owner of the file or to what group the directory belongs, their size, and at what
date they were created.
• In case we also want information about the hidden files (both hidden and unhidden files) we use
the command : ls –la (or al)
4. cp Command
• Stands for copy
a) Used for copying a file by changing its name
cp file’s old name file’s brand new name
b) Used to copy a file into another folder
cp file’s name /in which directory we are/username/file’s name to copy in
Example 1:
• First, we entered the folder documents and found in it a file named ‘test’
• Now we are going to copy the same file in the same folder but give it a
brand new name.
• So we will have in the file ‘documents’ two files; one which is called ‘test’
and the other ‘testcopy’
Example 2:
• First, we write cp name of the file to be copied then to where we want to
copy it.
5. rm Command
• Stands for ‘remove’
• Used to remove a file in our current folder.
• How to write the command:
• rm testcopy
rm Command (2)
• In case we want to delete a folder which contains existing files in it, we
use the same command but in a different way.
rm (space) -r (space) name of the folder which contains files
6. mkdir Command
• The mkdir command is used to create new directories (folders) on your
Linux system.
mkdir name of the new file
Examples of How to Use the mkdir Option:
1. mkdir -p IT403/2023/hw: This command will create a new directory called "IT403" and two
subdirectories called "2023" and "hw" inside it. The -p option tells mkdir to create any parent
directories that don't exist yet.
2. mkdir -pvm777 xxx/yyy/zzz: This command will create a new directory called "xxx" and two
subdirectories called "yyy" and "zzz" inside it, and set the file permissions to full read, write, and
execute for all users (777). The -p option again creates any necessary parent directories, and the -m
option sets the file permissions.

3. mkdir -v newdir: This command will create a new directory called "newdir" and print a message to the
terminal indicating that the directory was created. The -v option stands for "verbose" and tells mkdir to
be more talkative about what it's doing.
7. rmdir Command
• Used to delete a file that we created.
rmdir name of file to be deleted
8. man Command
• Stands for manual or tutorials
• Used to find out about any command we are using.
man (space) the command that we want to know about how to use it
man Command (2)
• man <command> to get help with this command
• man –k <keyword> to find all commands with that keyword
• To continue in the man command press Enter
• To quit the man command press Q
• Another tool you can use in a textual environment is the --help option
9. ls –p Command
• Lists the contents of a directory and adds a trailing slash (/) to directories to differentiate
them from regular files.
ls –p /home/user/documents
• The ‘-p’ option when used on a file path instead of a directory path, it will only list the
attributes of the file.
Example:
• In this example, the ‘ls –p’ command is used to list the contents of the
‘home/user/documents/’ directory.
• The files ‘file1.txt’, ‘file2.txt’, and ‘file3.txt’ are listed.
• ‘file2.txt’ and ‘file3.txt’ are listed with a trailing slash to indicate that they are directories.
Example(continue);
• However, if we try to apply the ‘ls –p’ command on a file path, such as:

• The command will only list the attributes of the file ‘file1.txt’ without
adding a trailing slash or any other special formatting.
10. ls -s Command
• Lists the contents of a directory along with their disk usage in
blocks.
• When used with a file path, the command will display the disk
usage of the file in blocks.
ls –s /home/user/documents/file1.txt
Example:

• In this example, the number 4 indicates that the file uses 4 disk blocks.
Note: The disk usage reported by ‘ls –r’ may differ from the actual file size.
• For example, if a file is 2KB in size and the block size is 4 KB, the ‘ls –s’ command
will report that the file occupies 1 block ( 4 KB) even though it actually occupies
only half a block (2KB).
11. ls –r Command
• Used to list the contents of a directory in reverse order.
• So files and directories are being listed in descending order based on their modification
time.
ls –r /home/user/documents/
In the following example ‘file3.txt’ is the most recently modified file and ‘file1.txt’ is the
least recently modified file.
13. ls –lh Command
• Lists the files and directories in the current directory, along with
their detailed information such as permissions, size, and
ownership, in a human-readable format.
ls -lh
Example:
1. Open the terminal on your Linux system.
2. Navigate to the directory you want to list the contents of. For example, if
you want to list the contents of the home directory, you can use the
following command:
Example (continue):
3 . Once you are in the directory, type the following command:

This will display the list of files and directories in the current directory,
along with their detailed information such as permissions, size, and
ownership, in a human-readable format.
Example(continue):
Example output:

In the example output above, the first column shows the file permissions, the second column shows the
number of links to the file or directory, the third and fourth columns show the user and group that owns the
file or directory, the fifth column shows the file size, and the last column shows the date and time the file
was last modified.
14. ls –a Command
• It’s an abbreviation to ls -all
• Lists all files and directories including the hidden ones.
• The files which have a dot before their name are considered hidden files or directories.
• Hidden files can’t be seen when running an ‘ls’ command.
ls –a
15. ls –t Command

• Lists the files and directories in the current directory,


sorted by the time they were last modified, with the
most recently modified files or directories appearing
first.
Example:
Here’s an example of how to use the ‘ls –t’ command:
1. Open the terminal on your Linux system.
2. Navigate to the directory you want to list the contents of. For example, if you want to
list the contents of the home directory, you can use the following command:
Example(continue):
3. Once you are in the directory, type the following command:

Example output:
16. ls –ltr Command
• Used to list the files and directories in a directory in the order of their last
modification time, with the least recently modified files or directories
appearing at the top.
• The ‘l’ option provides detailed information about the files and directories.
• The ‘r’ option reverses the order of the listing.
Example:
Here’s an example of how to use the ‘ls –ltr’ command:
1. Open the terminal on your Linux system.
2. Navigate to the directory you want to list the contents of. For example, if you want to
list the contents of the home directory, you can use the following command:
Example(continue):
3. Once you are in the directory , type the following command:
Example(continue):
Example output:
17. ls –m Command

• Used to list the files and directories in a directory in a


comma-separated format.
• This format is useful when you want to copy and paste
the list of files and directories to another program or
document.
Example:
18. ls -1 Command

• Used to list the files and directories in a directory with


one file or directory per line.
• This format is useful when you want to easily read and
parse the list of files and directories.
Example:
19. ls –R Command
• Used to list the files and directories in a directory recursively , i.e. , it lists
the contents of subdirectories as well.
Continue…
20. ls ~ or ls $HOME Command
• ~ : Used as a shorthand for the user’s home directory.
• Listing the contents of the home directory is simply ‘ls ~’ or ‘ls $HOME’.
21. cat Command

• Used for two purposes:


1. To display the contents of the file on the
terminal.
2. To concatenate(combine) multiple files and
display their contents on the terminal.
Example 1:
Here’s an example of how to use the ‘cat’ command to display the content of a file:
1. Open the terminal on your Linux system.
2. Navigate to the directory where the file is located. For example, if you want to display
the contents of a file named ‘example.txt’ in your home directory, you can use the
following command:
Continue…
3. Once you are in the directory, type the following command:

This will display the contents of the ‘example.txt’ file on the terminal.
Example output:
Example 2:
Here’s an example of how to use the ‘cat’ command to concatenate multiple files:
1. Open the terminal on your Linux system.
2. Navigate to the directory where the files are located. For example, if you want to
concatenate two files named ‘file1.txt’ and ‘file2.txt’ in your home directory, you can
use the following command:
Continue…
3. Once you are in the directory, type the following command:

This will display the content of the two files concatenated together on the terminal.
Example Output:
22. tac Command

• The ‘tac’ command in Linux is the reverse of the


‘cat’ command.
• It displays the content of a file in reverse order,
starting with the last line of the file and ending
with the first file.
Example:
23. more Command
• Used to view the contents of a file one screen at a time.
• It allows you to scroll through a file and view its contents in a more
organized and readable way.
Example:
• To scroll through the file, use the ‘Enter’ key to display the next screen of the file.
• To quit the ‘more’ command and return to the command prompt, use the ‘q’
command.
24. less Command

• The ‘less’ command in Linux is similar to the


‘more’ , but with more advanced features such as
scrolling through a file in both directions,
searching for text within a file, and opening files
in read-only mode.
• When using less, you can navigate through the file using the keyboard. For example, the ‘n’ key allows
you to move to the next page of the file, while the ‘p’ key allows you to move to the previous page.
• Here's how to use the n and p keys when viewing a file with the less command:
1. Open a terminal window on your Linux machine.
2. Navigate to the directory where the file you want to view is located. You can use the cd command to change
directories.
3. Type less followed by the name of the file you want to view. For example, if the file is named example.txt,
you would type less example.txt.
4. The file will be displayed one screen at a time. To move to the next screen, press the n key on your
keyboard. To move to the previous screen, press the p key.
Example:
Example:
• To scroll through the file, use the ‘Up’ and ‘Down’ arrow keys or the ‘Page
Up’ and ‘Page Down’ keys to move up and down the file.
• To search for a specific string within the file, use the ‘/’ key followed by the
string you want to search for.
• For example, to search for the word “example” within the file, type
‘/example’ and press ‘Enter’.
• To quit the ‘less’ command and return to the command prompt, use the ‘q’
key.
25. head/ tail Command
• head command is used to read the file from the beginning and the
• tail command is used to read the file from the ending
• (by default it’s 10 lines and it accepts multiple files)
-n x
• -n x : prints the first x lines: This option allows you to specify the
number of lines you want to display from the beginning of a file. For
example, to display the first 10 lines of a file named "exam-n x : prints the
first x lines: This option allows you to specify the number of lines you
want to display from the beginning of a file. For example, to display the
first 10 lines of a file named "example.txt", you can use the command
‘head -n 10 example.txt’.
-n -x
• -n -x : head : omits x last lines; tail : no changes when using the -:
When used with the ‘head’ command, this option allows you to omit the
last x lines from the end of a file. For example, to display all lines of a file
except the last 5, you can use the command ‘head -n -5 example.txt’.
When used with the ‘tail’ command, this option has no effect.
-c x
• -c x : prints the first x characters: This option allows you to specify the
number of characters you want to display from the beginning of a file. For
example, to display the first 100 characters of a file named "example.txt",
you can use the command ‘head -c 100 example.txt’.
-c x
• -c x : prints the first x characters: This option allows you to specify the
number of characters you want to display from the beginning of a file. For
example, to display the first 100 characters of a file named "example.txt",
you can use the command ‘head -c 100 example.txt’.
-c x
• -c x : prints the first x characters: This option allows you to specify the
number of characters you want to display from the beginning of a file. For
example, to display the first 100 characters of a file named "example.txt",
you can use the command ‘head -c 100 example.txt’.
tail -f
• tail -f : used to monitor the log entries written by running programs:
This command is used to monitor the log entries written by running
programs. The -f option allows you to continuously display the last few
lines of a file as new lines are added to it. For example, to monitor the log
file "example.log" in real-time, you can use the command ‘tail -f
example.log’ This is useful for debugging or monitoring programs that
generate a lot of output.
27. clear Command
• Clears everything we wrote from the beginning.
clear
28. df Command
• Stands for disk free
• Used to display information about the disk space usage on the
file system.
• It shows the total amount of disk space, the amount of space
used, the amount of space available, and the percentage of
space used on each mounted system.
Example 1:
Example 2:
• To display the disk space usage information in a human-readable format,
displaying sizes in kilobytes, megabytes, and gigabytes, use the ‘-h’
option. For example:
29. tar Command
• Used to create and manipulate archives in the tar format.
• The tar format is a standard Unix file format for archiving files
and directories into a single file, often called a tarball.
Example (creating a tarball):
• To create a tarball of a directory, use the ‘tar’ command with the ‘-cvf’ options followed
by the name of the tarball file and the name of the directory to be archived.
• For example, to create a tarball of a directory called ‘myfolder’, use the following
command:

• This will create a file called ‘myfolder.tar’ containing all the files and subdirectories
within the ‘myfolder’ directory.
Example (Extracting a tarball):
• To extract the contents of a tarball :
tar –xvf name of the tarball file
• For example, to extract the contents of the ‘myfolder.tar’ tarball created in the
previous step, use the following command:

• This will extract all the files and subdirectories from the ‘myfolder.tar’ tarball file
into the current directory.
30. locate Command
• Used to search for files and directories on the system based on
their names or paths.
• It searches a pre-built database of file names and locations,
which makes it faster than searching the file system directly.
Example (updating the locate database):
• Before using the ‘locate’ command, you need to update its database to
ensure that it is up-to-date with the latest file system changes.
• To update the database , use the following command:

• This will update the database of file names and locations on the system.
Example (using the locate command):
• Once the database is updated, you can use the ‘locate’ command to search
for files and directories.

• The command will display a list of all the files and directories that match
the search criteria, including their paths.
Continue…
• You can use wildcards with the ‘locate’ command to search for files or
directories that match a pattern.
• For example, to search for all files with the extension “.txt” in the system,
you can use the following command:
Locate Command line Options

• -c
• -i
• -lx
• -b file
-c
• -c : write the number of matching entries only The "-c" option is used to
count the number of matching entries in a search query. For example, if
you run a search for all files with the extension ".txt" in a folder, the "-c"
option will display only the number of files that match that criteria, rather
than the full list of files.
• Example: Suppose you run the command "ls *.txt -c" in a folder
containing 10 text files. The output will be "10", indicating that there are
10 files with the extension ".txt".
-i
• -i : ignore case matching The "-i" option is used to ignore case when
performing a search query. This means that the search will match results
regardless of whether the text is in uppercase or lowercase.
• Example: If you run the command "grep -i hello file.txt", it will search for
all occurrences of the word "hello" in the file "file.txt", regardless of
whether it is written as "Hello", "HELLO", or "hello".
-lx
• -lx : limit the results to x The "-lx" option is used to limit the number of
results displayed in a search query to x. This is useful when you want to
avoid a long list of results and only want to see a specific number of them.
• Example: Suppose you run the command "find . -name "*.txt" -maxdepth
1 -lx 3" in a folder containing 10 text files. The output will be limited to
the first 3 files that match the criteria (i.e., have the extension ".txt" and
are located in the current directory).
-b file
• -b file : searches for files and directories whose names match the search
pattern only at the base of their file path The "-b" option is used to search for
files and directories whose names match a search pattern only at the base of
their file path. This means that it will only search for files and directories in
the current directory and not in any subdirectories.
• Example: Suppose you run the command "ls -b *.txt" in a folder containing
10 text files and 2 subdirectories. The output will display only the text files
that are located in the current directory, not in any of the subdirectories
31. bzip2 compression
• The gzip and bzip2 are both compression utilities that can be used to
compress and decompress files. In the context of creating compressed
archive files using tar, gzip and bzip2 are commonly used to reduce the
size of the archive.
• The most frequently used compression method with tar is gzip, which is used to create
compressed archive files in the .tar.gz format. To create a compressed archive file using gzip,
you can use the following command:

• In this command, the -c option specifies that a new archive should be created, -z specifies that
the archive should be compressed with gzip, -v enables verbose mode to display progress in
the terminal, and -f specifies the name of the archive file to be created. archive.tar.gz is the
name of the archive file, and files is the name of the directory or file to be included in the
archive.
• Alternatively, you can use bzip2 compression to create compressed archive files
in the .tar.bz2 format. To create a compressed archive file using bzip2, you can
use the following command:

• In this command, the only difference is the -j option, which tells tar to use bzip2
compression instead of gzip. The resulting archive file will have a .tar.bz2
extension, and the compression ratio will generally be higher than with gzip.
• In summary, gzip is faster but compresses slightly less, while bzip2 is
slower but compresses more. The choice between the two depends on the
specific use case and the balance between speed and compression ratio
required.
Example (to compress a file):
• The command will create a new compressed file called ‘myfile.txt.bz2’
In the same directory as the original file. The original file will be replaced with the
compressed file.

• We can also use the ‘-k’ option to keep the original file and create a compressed file with
a different name. Example:
Example(to decompress a file):
• To decompress a file that has been compressed with bzip2, we can use the ‘bunzip2’ command.
• In the following example, the command will decompress the compressed file ‘myfile.txt.bzip2’
and create a new file called ‘myfile.txt’ in the same directory.

• We can also use the ‘-k’ option with ‘bunzip2’ to keep the compressed file and create a new
decompressed file with a different name, for example:
32. tar Command
• The ‘tar’ command in Linux is used for creating, viewing, and extracting
tar archives.
• A tar archive is a collection of files and directories that are bundled
together into a single file for easier storage, distribution, and backup.
• The command "tar -czvf name-of-archive.tar.gz /path/to/directory-or-file"
is used to create a compressed archive file in the .tar.gz format.
• Here is the explanation of the options used in the command:

• -c: This option stands for "create" and instructs the tar command to create a new archive.

• -z: This option tells the tar command to compress the archive using the gzip utility. Without this option, the tar command would create
an uncompressed archive.

• -v: This option stands for "verbose" and tells the tar command to display the progress of the archive creation process in the terminal.
It's an optional flag, but it can be helpful to see what's happening during the archive creation process.

• -f: This option specifies the name of the archive file to be created. The f stands for "file". You need to specify the name of the archive
immediately after the -f option, followed by the path to the directory or file you want to include in the archive.
• So, in summary, the command "tar -czvf name-of-archive.tar.gz
/path/to/directory-or-file" creates a new compressed archive file named
"name-of-archive.tar.gz" containing the contents of the directory or file
specified by the "/path/to/directory-or-file" argument. The archive is
compressed using the gzip utility, and the progress of the archive creation
process is displayed in the terminal.
• The tar command accepts multiple files / directories
➔ tar -czvf archive.tar.gz file1 folder1/ file2 …
• Exclude Directories and Files
➔ --exclude=/folder ; --exclude=file
➔ tar -czvf archive.tar.gz /home/ubuntu --exclude=*.mp4
32. Extracting a tar
Example 1(extract an archive):

• This command extracts the contents of the ‘archive.tar.gz’ file in the current directory.
• ‘-x’ : specifies that we want to extract the archive.
• ‘-z’: specifies that the archive is compressed with gzip.
• ‘-v’: enables verbose output
• ‘-f’: specifies the name of the archive file.
Example 2(extract to a specific
directory):

• The command extracts the contents of the ‘archive.tar.gz’ file to the


‘/tmp’ directory.
• The ‘-c’ option specifies the target directory for the extraction.
Example 3(show contents of an archive):

• This command lists the contents of the ‘archive.tar.gz’ file without


extracting it.
• ‘-t’: specifies that we want to list the contents.
• ‘-v’: enables verbose output.
• ‘-f’: specifies the name of the archive file.

You might also like