0% found this document useful (0 votes)
4 views23 pages

Module 4 – Linux Fundamentals

Module 4 covers essential Linux commands, including their syntax and usage for file management, permissions, ownership, and help commands. Key commands such as pwd, mkdir, chmod, and chown are explained, along with file permissions and ownership management. The module also introduces help commands like man and whatis for user assistance in navigating Linux commands.

Uploaded by

Gaurav Dubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

Module 4 – Linux Fundamentals

Module 4 covers essential Linux commands, including their syntax and usage for file management, permissions, ownership, and help commands. Key commands such as pwd, mkdir, chmod, and chown are explained, along with file permissions and ownership management. The module also introduces help commands like man and whatis for user assistance in navigating Linux commands.

Uploaded by

Gaurav Dubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Module 4 – Linux Fundamentals

 Commands Syntax
The following are the top 50 Linux commands Syntax:
1. pwd Command
The pwd command is used to display the location of the current working directory.
Syntax:
pwd
2. mkdir Command
The mkdir command is used to create a new directory under any directory.
Syntax:
mkdir <directory name>
3. rmdir Command
The rmdir command is used to delete a directory.
Syntax:
rmdir <directory name>
4. ls Command
The ls command is used to display a list of content of a directory.
Syntax:
ls

5. cd Command
The cd command is used to change the current directory.
Syntax:
cd <directory name>
6. touch Command
The touch command is used to create empty files. We can create multiple empty files by
executing it once.
Syntax:
touch <file name>
touch <file1> <file2> ....
7. cat Command
The cat command is a multi-purpose utility in the Linux system. It can be used to create a
file, display content of the file, copy the content of one file to another file, and more.
Syntax:
cat [OPTION]... [FILE]..
To create a file, execute it as follows:
cat > <file name>
// Enter file content
Press "CTRL+ D" keys to save the file. To display the content of the file, execute it as
follows:
cat <file name>
8. rm Command
The rm command is used to remove a file.
Syntax:
rm <file name>
9. cp Command
The cp command is used to copy a file or directory.
Syntax:
To copy in the same directory:
cp <existing file name> <new file name>
10. mv Command
The mv command is used to move a file or a directory form one location to another
location.
Syntax:
mv <file name> <directory path>
11. rename Command
The rename command is used to rename files. It is useful for renaming a large group of
files.
Syntax:
rename 's/old-name/new-name/' files
12. head Command
The head command is used to display the content of a file. It displays the first 10 lines of
a file.
Syntax:
head <file name>
13. tail Command
The tail command is similar to the head command. The difference between both
commands is that it displays the last ten lines of the file content. It is useful for reading
the error message.
Syntax:
tail <file name>
14. tac Command
The tac command is the reverse of cat command, as its name specified. It displays the file
content in reverse order (from the last line).
Syntax:
tac <file name>
15. more command
The more command is quite similar to the cat command, as it is used to display the
file content in the same way that the cat command does. The only difference
between both commands is that, in case of larger files, the more command
displays screenful output at a time.
In more command, the following keys are used to scroll the page:
ENTER key: To scroll down page by line.
Space bar: To move to the next page.
b key: To move to the previous page.
/ key: To search the string.
Syntax:
1. more <file name>
16. less Command
The less command is similar to the more command. It also includes some extra features
such as 'adjustment in width and height of the terminal.' Comparatively, the more
command cuts the output in the width of the terminal.
Syntax:
less <file name>
17. su Command
The su command provides administrative access to another user. In other words, it allows
access of the Linux shell to another user.
Syntax:
su <user name>
18. id Command
The id command is used to display the user ID (UID) and group ID (GID).
Syntax:
id
19. useradd Command
The useradd command is used to add or remove a user on a Linux server.
Syntax:
useradd username
20. passwd Command
The passwd command is used to create and change the password for a user.
Syntax:
passwd <username>
21. groupadd Command
The groupadd command is used to create a user group.
Syntax:
groupadd <group name>
22. cat Command
The cat command is also used as a filter. To filter a file, it is used inside pipes.
Syntax:
cat <fileName> | cat or tac | cat or tac |. . .
23. cut Command
The cut command is used to select a specific column of a file. The '-d' option is used as a
delimiter, and it can be a space (' '), a slash (/), a hyphen (-), or anything else. And, the '-f'
option is used to specify a column number.
Syntax:
cut -d(delimiter) -f(columnNumber) <fileName>

24. grep Command


The grep is the most powerful and used filter in a Linux system. The 'grep' stands for
"global regular expression print." It is useful for searching the content from a file.
Generally, it is used with the pipe.
Syntax:
command | grep <searchWord>
25. comm Command
The 'comm' command is used to compare two files or streams. By default, it displays
three columns, first displays non-matching items of the first file, second indicates the
non-matching item of the second file, and the third column displays the matching items of
both files.
Syntax:
comm <file1> <file2>
26. sed command
The sed command is also known as stream editor. It is used to edit files using a regular
expression. It does not permanently edit files; instead, the edited content remains only on
display. It does not affect the actual file.
Syntax:
command | sed 's/<oldWord>/<newWord>/'
27. tee command
The tee command is quite similar to the cat command. The only difference between both
filters is that it puts standard input on standard output and also write them into a file.
Syntax:
cat <fileName> | tee <newFile> | cat or tac |.....
28. tr Command
The tr command is used to translate the file content like from lower case to upper case.
Syntax:
command | tr <'old'> <'new'>
29. uniq Command
The uniq command is used to form a sorted list in which every word will occur only once.
Syntax:
command <fileName> | uniq
30. wc Command
The wc command is used to count the lines, words, and characters in a file.
Syntax:
wc <file name>
31. od Command
The od command is used to display the content of a file in different s, such as
hexadecimal, octal, and ASCII characters.
Syntax:
od -b <fileName> // Octal format
od -t x1 <fileName> // Hexa decimal format
od -c <fileName> // ASCII character format
32. sort Command
The sort command is used to sort files in alphabetical order.
Syntax:
sort <file name>
33. gzip Command
The gzip command is used to truncate the file size. It is a compressing tool. It replaces the
original file by the compressed file having '.gz' extension.
Syntax:
Gzipo787 <file1> <file2> <file3>...
34. gunzip Command
The gunzip command is used to decompress a file. It is a reverse operation of gzip
command.
Syntax:
gunzip <file1> <file2> <file3>. .
35. find Command
The find command is used to find a particular file within a directory. It also supports
various options to find a file such as byname, by type, by date, and more.
The following symbols are used after the find command:
(.): For current directory name
(/): For root
Syntax:
find. -name "*.pdf"
36. locate Command
The locate command is used to search a file-by-file name. It is quite similar to find
command; the difference is that it is a background process. It searches the file in the
database, whereas the find command searches in the file system. It is faster than the find
command. To find the file with the locates command, keep your database updated.
Syntax:
locate <file name>
37. date Command
The date command is used to display date, time, time zone, and more.
Syntax:
date
38. cal Command
The cal command is used to display the current month's calendar with the current date
highlighted.
Syntax:
cal<

39. sleep Command


The sleep command is used to hold the terminal by the specified amount of time. By
default, it takes time in seconds.
Syntax:
sleep <time>
40. time Command
The time command is used to display the time to execute a command.
Syntax:
time
41. zcat Command
The zcat command is used to display the compressed files.
Syntax:
zcat <file name>
42. df Command
The df command is used to display the disk space used in the file system. It displays the
output as in the number of used blocks, available blocks, and the mounted directory.
Syntax:
df
43. mount Command
The mount command is used to connect an external device file system to the system's file
system.
Syntax:
mount -t type <device> <directory>
44. exit Command
Linux exit command is used to exit from the current shell. It takes a parameter as a
number and exits the shell with a return of status number.
Syntax:
exit
45. clear Command
Linux clear command is used to clear the terminal screen.
Syntax:
clear
46. ip Command
Linux ip command is an updated version of the ipconfig command. It is used to assign an
IP address, initialize an interface, disable an interface.
Syntax:
ip a or ip addr
47. ssh Command
Linux ssh command is used to create a remote connection through the ssh protocol.
Syntax:
ssh user_name@host(IP/Domain_name)</p>
48. mail Command
The mail command is used to send emails from the command line.
Syntax:
mail -s "Subject" <recipient address>
49. ping Command
The ping command is used to check the connectivity between two nodes, that is whether
the server is connected. It is a short form of "Packet Internet Groper."
Syntax:
ping <destination>
50. host Command
The host command is used to display the IP address for a given domain name and vice
versa. It performs the DNS lookups for the DNS Query.
Syntax:
host <domain name> or <ip address>
 File Permissions (chmod)
In Linux, file permissions are managed using the `chmod` command, which stands for
"change mode." It allows users to change the access permissions of files and directories.
File permissions in Linux are divided into three sets, each representing a different
category of users:
1. Owner (User): The user who owns the file.
2. Group: Users who are members of the group associated with the file.
3. Others (World): Any other user on the system who is not the owner or a member of the
group.
Each of these categories has three types of permissions:
1. Read (`r`): Allows a user to view the contents of a file or list the contents of a
directory.
2. Write (`w`): Allows a user to modify the contents of a file or create, delete, and rename
files within a directory.
3. Execute (`x`): For files, allows a user to exe
cute the file if it is a program or script. For directories, allows a user to access the
directory and its contents.
Syntax:
The syntax of the `chmod` command is as follows:

Bash
chmod [options] mode file
-`[options]`: Optional parameters.
- `mode`: Specifies the permissions to set using either symbolic or octal notation.
-`file`: Specifies the file or directory whose permissions are to be changed.
Symbolic Notation:
Symbolic notation allows you to modify permissions based on the user category (`u` for
user/owner, `g` for group, `o` for others) and the type of permission (`+` to add, `-` to
remove, `=` to set).
For example:
- `chmod u+x file.txt`: Adds execute permission for the owner of the file.
- `chmod go-w file.txt`: Removes write permission for both the group and others.
- `chmod u=rw,go=r file.txt`: Sets read and write permissions for the owner and read-only
permissions for the group and others.
Octal Notation:
Octal notation represents permissions as a three-digit number, where each digit
corresponds to the permissions of the owner, group, and others, respectively.
- `4` (read permission)
- `2` (write permission)
- `1` (execute permission)

To calculate the permission value, sum up the numbers for each type of permission. For
example:
- `chmod 755 file.txt`: Sets read, write, and execute permissions for the owner, and read
and execute permissions for the group and others.
Additional Options:

`-R`: Recursively change permissions for directories and their contents.


`-v`: Verbose mode, which displays a message for each file processed.
`-f`: Suppress error messages.
`-c`: Displays a message only if the permissions of a file are changed.
 File Ownership (chown, chgrp)
In Linux, the ownership of files and directories is crucial for security and access control. The
`chown` and `chgrp` commands are used to change the ownership and group ownership of
files and directories, respectively.
Here's how you can use them:
`chown` Command:
`chown` stands for "change owner." It allows you to change the owner of a file or directory.
Syntax:
chown [OPTIONS] OWNER[:GROUP] FILE
- OPTIONS: Various options can be used with `chown` to modify its behavior.
- OWNER: Specifies the new owner of the file or directory.
- GROUP: Optionally, you can specify a new group for the file or directory. If not specified,
the group remains unchanged.
- FILE: Specifies the file or directory whose ownership is to be changed.
Example:
bash
# Change the owner of file.txt to user1
chown user1 file.txt
# Change both the owner and the group of file.txt to user2 and group1, respectively
chown user2:group1 file.txt
`chgrp` Command:
`chgrp` stands for "change group." It allows you to change the group ownership of a file or
directory.
Syntax:
chgrp [OPTIONS] GROUP FILE
- OPTIONS: Similar to `chown`, various options can modify `chgrp`'s behavior.
- GROUP: Specifies the new group for the file or directory.
- FILE: Specifies the file or directory whose group ownership is to be changed.
Example:
bash
Change the group of file.txt to group2
chgrp group2 file.txt
Permissions:
Remember, changing ownership/group doesn't affect permissions directly, but it might
indirectly affect them, especially if there are specific permissions set for the owner or group.
After changing ownership or group, you may need to adjust permissions using `chmod` to
ensure proper access control.
-These commands are powerful tools for managing file permissions and access control in
Linux systems. Always use them carefully, especially with administrative privileges, to
avoid unintended consequences.
 Getting Help (man, whatis etc.)
In Linux, you have several built-in commands to get help and information about commands
and their usage. Here are some commonly used ones:
1. man: The `man` command displays the manual pages for a particular command. For
example, to see the manual for the `ls` command, you would type:
man ls
You can navigate through the manual pages using the arrow keys, page up/down, and
searching with `/`.
2. whatis: The `whatis` command provides a brief description of a command. For example:
whatis ls
This will give you a short description of the `ls` command.
3. info: Some commands have their documentation in `info` format. You can use the `info`
command to access this documentation. For example:
info ls
This will display the `ls` command's documentation in the info format.
4. help option: Many commands support a `--help` option that provides a quick overview of
the command and its options. For example:
ls --help
This will display a brief help message for the `ls` command.
5. apropos: The `apropos` command searches the manual page names and descriptions for
the keyword you provide. For example:
apropos network
This will display a list of commands related to the keyword "network".

 TAB completion and up arrow keys


In Linux, the Tab completion and up arrow keys are essential for efficient command-line
navigation and execution.
1. Tab Completion: When you start typing a command or file path in the terminal and press
the Tab key, the system tries to auto-complete what you're typing based on existing
commands, files, or directories in the current context. If there's only one possibility, it
completes the input; if there are multiple options, it lists them for you to choose from.
For example, if you type `ls /usr/l` and press Tab, it might complete to `ls /usr/local/`,
assuming that's the only option.
2. Up Arrow Key: Pressing the up-arrow key on your keyboard recalls previous commands
you've entered. Each press moves you further back in your command history. This is useful
for re-executing commands without retyping them or for modifying past commands.
For instance, if you've previously entered `ls -l`, pressing the up-arrow key once will show
`ls -l` in your terminal, ready to be executed again. Pressing it multiple times will cycle
through your command history.
 Adding text to file
To add text to a file in Linux, you can use various methods, including using command-line
text editors like `nano`, `vim`, or `emacs`, or using command-line redirection operators like
`>` or `>>`. Here are some common methods:
1. Using `echo` with redirection (`>>`):
echo "Your text here" >> filename.txt
This will append the text to the end of the file or create the file if it doesn't exist.
2. Using a text editor like `nano`:
nano filename.txt
This will open the file in the nano text editor, where you can add or edit text. After adding
your text, press `Ctrl + O` to save and `Ctrl + X` to exit.
3. Using a text editor like `vim`:
vim filename.txt
This will open the file in the vim text editor. Press `i` to enter insert mode, add your text,
then press `Esc` followed by `:wq` to save and exit.
4. Using a text editor like `emacs`:
emacs filename.txt
This will open the file in the emacs text editor. Add your text, then press `Ctrl + x`,
followed by `Ctrl + s` to save and `Ctrl + x`, followed by `Ctrl + c` to exit.

 Standard output to a file (tee command)


The `tee` command in Linux is used to read from standard input and write to both standard
output and files. This is particularly useful when you want to see the output on your terminal
while simultaneously saving it to a file.
The basic syntax for using `tee` is:
command | tee filename
Here, `command` is any command whose output you want to capture, and `filename` is the
name of the file where you want to save the output.
For example, if you want to list the contents of a directory and save the output to a file
named "directory_contents.txt", you can use:
ls | tee directory_contents.txt
This command will display the output of `ls` on the terminal and also save it to the
"directory_contents.txt" file.
You can also use the `-a` option with `tee` to append output to the specified file instead of
overwriting it:
command | tee -a filename

 Pipes (|)
A pipe is a form of redirection (transfer of standard output to some other destination) that is
used in Linux and other Unix-like operating systems to send the output of one
command/program/process to another command/program/process for further processing. The
Unix/Linux systems allow the stdout of a command to be connected to the stdin of another
command. You can make it do so by using the pipe character ‘|’.
The pipe is used to combine two or more commands, and in this, the output of one command
acts as input to another command, and this command’s output may act as input to the next
command, and so on. It can also be visualized as a temporary connection between two or
more commands/ programs/ processes. The command line programs that do the further
processing are referred to as filters.
This direct connection between commands/ programs/ processes allows them to operate
simultaneously and permits data to be transferred between them continuously rather than
having to pass it through temporary text files or through the display screen.
Pipes are unidirectional i.e., data flows from left to right through the pipeline.
Syntax:
command_1 | command_2 | command_3 | .... | command_N
Example of Piping in Unix or Linux
1. List all files and directories and give them as input to `grep` command using piping
in Linux
ls | grep file.txt
2. List all files and directories and give them as input to `more` commands using piping
in Linux.
$ ls -l | more
3. Sort a list of files by size using piping in Linux
ls -l sort -k 5
4. Use sort and uniq command to sort a file and print unique values using piping in
Linux
$ sort filename | uniq
5. Use head and tail to print lines in a particular range in a file.
$ cat sample2.txt | head -7 | tail -5
6. Use ls and find to list and print all lines matching a particular pattern in matching
files.
$ ls -l | find ./ -type f -name "*.txt" -exec grep "program" {} \;
7. Use cat, grep, tee and wc command to read the particular entry from user and store
in a file and print line count.
$ cat result.txt | grep "Rajat Dua" | tee file2.txt | wc -l
8.How can I redirect the output of a piped command to file in Unix or Linux?
We can use redirection operator `>` to redirect the output of a piped command.
For Example:
If i have a file name `file.txt` and want to redirect it to a file name `geeks.txt`.
ls | grep 'file' > geeks.txt
 File Maintenance Commands
In Linux, there are several file maintenance commands used for managing files and
directories. Here's a list of some commonly used ones:
1. ls: Lists files and directories in the current directory.
ls
2. cd: Changes the current directory.
cd directory_name
3. pwd: Prints the current working directory.
pwd
4. mkdir: Creates a new directory.
mkdir directory_name
5. rmdir: Removes an empty directory.
rmdir directory_name
6. cp: Copies files and directories.
cp source_file destination_file
7. mv: Moves or renames files and directories.
mv source_file destination_file
8. rm: Removes files and directories.
rm file_name
9. touch: Creates an empty file or updates the access and modification times of a file.
touch file_name
10. chmod: Changes file permissions.
chmod permissions file_name

11. chown: Changes file owner and group.


chown owner: group file_name
12. ln: Creates links between files.
ln -s target_link link_name

13. find: Searches for files in a directory hierarchy.


find directory -name filename

14. grep: Searches for patterns in files.


grep pattern file_name

15. tar: Archives files into a tarball.


tar options archive_name files

 File Display Commands


In Linux, several commands are used to display the content of files or directories. Here are
some commonly used ones:

1. cat: Short for concatenate, this command is used to display the content of one or more
files. For example:
cat filename
2. head: This command is used to display the first few lines of a file. By default, it shows the
first 10 lines. You can specify a different number of lines with the `-n` option. For example:
head -n 5 filename
3. tail: Similar to `head`, but it displays the last few lines of a file. By default, it shows the
last 10 lines. You can also specify a different number of lines with the `-n` option. For
example:
tail -n 5 filename
4. more: This command displays the content of a file one screen at a time. You can navigate
through the file using the Enter key. For example:
more filename
5. less: Similar to `more`, but it allows backward navigation as well. It's more versatile and
efficient for large files. For example:
less filename
6. nl: This command is used to display the content of a file with line numbers. For example:
nl filename
7. grep: While primarily used for searching text patterns, `grep` can also be used to display
lines matching a pattern in a file. For example:
grep "pattern" filename
8. echo: Although primarily used for printing messages, `echo` can also be used to display
the content of variables or strings. For example:
echo "Hello, world!"

 Filters / Text Processing Commands


Text Processing Commands are a set of built-in commands that are used to manipulate text.
These commands allow users to quickly and efficiently search, modify, and extract data from
text files.
Command Description Examples

cat Concatenate and display files cat file1.txt file2.txt

sort Sort lines of text files sort file.txt

uniq Remove duplicate lines from a sorted file sort file.txt | uniq

grep Search for patterns in files grep "pattern" file.txt

cut Extract columns of text from files cut -f1,3 file.txt

Stream editor for filtering and transforming


sed sed 's/old/new/' file.txt
text

awk Pattern scanning and processing language awk '{print $1, $3}' file.txt

tr Translate or delete characters tr 'a-z' 'A-Z' < file.txt

wc Count lines, words, and characters in a file wc file.txt

diff Compare two files and show differences diff file1.txt file2.txt

patch Apply a diff file to a file or directory patch file.txt patch.diff

nl Number lines in a file nl file.txt

head Display the first few lines of a file head file.txt

tail Display the last few lines of a file tail file.txt

tee Redirect output to a file and to the terminal ls | tee output.txt

fmt Format text files for printing fmt file.txt


Command Description Examples

pr Convert text files for printing pr file.txt

iconv -f utf-8 -t iso-8859-1


iconv Convert character encoding of a file file.txt

Convert DOS line endings to UNIX line


dos2unix dos2unix file.txt
endings

rev Reverse lines of a file rev file.txt

fold Wrap lines of text to a specified width fold -w 80 file.txt

Join lines from two files based on a common


join join file1.txt file2.txt
field

 Compare Files
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you
to find out whether the two files are identical or not.
When cmp is used for comparison between two files, it reports the location of the first
mismatch to the screen if difference is found and if no difference is found i.e the files
compared are identical.
cmp displays no message and simply returns the prompt if the the files compared are
identical.
Syntax:
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]

diff stands for difference. This command is used to display the differences in the files by
comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which
lines in one file have is to be changed to make the two files identical.
The important thing to remember is that diff uses certain special symbols and instructions
that are required to make two files identical. It tells you the instructions on how to change
the first file to make it match the second file.

Syntax :
diff [options] File1 File2

 Compress and un-compress files/directories


Compressing files
Syntax Description Example(s)

Gzip compress the size of the


given files using Lempel-Ziv gzip mydata.doc
gzip {filename} coding (LZ77). Whenever gzip *.jpg
possible, each file is replaced by ls -l
one with the extension .gz.

bzip2 compresses files using the


Burrows-Wheeler block sorting
text compression algorithm, and
Huffman coding. Compression is
bzip2 mydata.doc
generally considerably better than
bzip2 {filename} bzip2 *.jpg
that achieved by bzip command
ls -l
(LZ77/LZ78-based compressors).
Whenever possible, each file is
replaced by one with the extension
.bz2.

zip is a compression and file


packaging utility for Unix/Linux. zip mydata.zip mydata.doc
zip {.zip-filename} {filename-to-
Each file is stored in single .zip zip data.zip *.doc
compress}
{.zip-filename} file with the ls -l
extension .zip.

The GNU tar is archiving utility but


it can be use to compressing large
file(s). GNU tar supports both
tar -zcvf data.tgz *.doc
archive compressing through gzip
tar -zcvf {.tgz-file} {files} tar -zcvf pics.tar.gz *.jpg *.png
and bzip2. If you have more than 2
tar -jcvf {.tbz2-file} {files} tar -jcvf data.tbz2 *.doc
files then it is recommended to use
ls -l
tar instead of gzip or bzip2.
-z: use gzip compress
-j: use bzip2 compress

Decompressing files
Syntax Description Example(s)

Decompressed a file that is


gzip -d {.gz file} created using gzip command. File gzip -d mydata.doc.gz
gunzip {.gz file} is restored to their original form gunzip mydata.doc.gz
using this command.

Decompressed a file that is


bzip2 -d {.bz2-file} created using bzip2 command. bzip2 -d mydata.doc.bz2
bunzip2 {.bz2-file} File is restored to their original gunzip mydata.doc.bz2
form using this command.

Extract compressed files in unzip file.zip


unzip {.zip file}
a ZIP archive. unzip data.zip resume.doc

Untar or decompressed a file(s)


tar -zxvf data.tgz
tar -zxvf {.tgz-file} that is created
tar -zxvf pics.tar.gz *.jpg
tar -jxvf {.tbz2-file} using tar compressing
tar -jxvf data.tbz2
through gzip and bzip2 filter
 Truncate file size (truncate)
In Linux, the `truncate` command is used to shrink or extend the size of a file to a specified
size. Here's how you can use it:
bash
truncate -s <size> <file_name>

Replace `<size>` with the desired size for the file, and `<file_name>` with the name of the
file you want to truncate.
For example, to truncate a file named `example.txt` to 1000 bytes:
bash
truncate -s 1000 example.txt

This command will truncate the file `example.txt` to 1000 bytes. If the file is already smaller
than the specified size, it will be left unchanged. If the file is larger than the specified size, it
will be truncated to that size, with any extra data being discarded.

If you want to extend a file, you can use the `-o` (or `--no-create`) option:
bash
truncate -s +1000 example.txt

This command will extend the file `example.txt` by 1000 bytes, adding null bytes at the end
if necessary.
 Combining and Splitting Files (cat and split)
In Linux, you can combine and split files using the `cat` and `split` commands.

Combining Files with `cat`:


The `cat` command is used to concatenate files. To combine multiple files into one, you can
simply use `cat` followed by the file names, then redirect the output to a new file. Here's an
example:
bash
cat file1.txt file2.txt > combined_file.txt

This command concatenates `file1.txt` and `file2.txt` and writes the combined content to
`combined_file.txt`.

Splitting Files with `split`:


The `split` command is used to split files into smaller parts. You can specify the size of each
part or the number of lines per part. Here's how you can use it:
bash
split -b <size> <input_file> <output_prefix>

Replace `<size>` with the desired size for each part (e.g., 1M for 1 megabyte), `<input_file>`
with the name of the file you want to split, and `<output_prefix>` with the prefix for the
output files.
For example, to split a file named `large_file.txt` into 100 MB parts:
bash
split -b 100M large_file.txt part_

This command will create multiple files with names like `part_aa`, `part_ab`, `part_ac`, etc.,
each containing 100 MB of data from `large_file.txt`.
You can also specify the number of lines per part using the `-l` option:
bash
split -l <lines> <input_file> <output_prefix>

Replace `<lines>` with the desired number of lines per part.


 Linux vs Windows Commands
Most of us think that Linux has terminal and we can use a command-line interface only in
Linux but it is just a myth. There is a PowerShell and a command prompt in windows as
well where we may execute the commands easily. But Windows and Linux have
commands with the same name as well.

SNo.
Windows Linux Description

1. dir ls -l Directory listing

2. ren mv Rename a file

3. copy cp Copying a file

4. move mv Moving a file

5. cls clear Clear Screen

6. del rm Delete file

7. fc diff Compare contents of files

8. find grep Search for a string in a file

Display the manual/help details


9. command /? man command
of the command

Returns your current directory


10. chdir pwd
location

11. time date Displays the time

12. cd cd Change the current directory

13. md mkdir To create a new directory/folder

14. echo echo To print something on the screen

vim(depends on
15. edit To write in to files.
editor)

16. exit exit To leave the terminal/command


SNo.
Windows Linux Description

window.

mke2fs or
17. format To format a drive/partition.
mformat

18. free mem To display free space.

19. rmdir rm -rf/rmdir To delete a directory.

20. taskkill kill To kill a task.

21. tasklist ps x To list running tasks.

22. set var=value export var=value To set environment variables.

23. attrib chown/chmod To change file permissions.

To print the route packets trace


24. tracert traceroute
to network host.

daemon to execute scheduled


25. at cron
commands.

26. type cat To print contents of a file.

To send ICMP ECHO_REQUEST to


27. ping ping
network hosts.

To query Internet name servers


28. nslookup nslookup
interactively.

29. chdisk du -s For disk usage.

30. tree ls -R To list directory recursively.

You might also like