linux commands
linux commands
CHMOD
The chmod command in Linux is used to change the permissions of a file or directory. Permissions
determine who can read, write, or execute a file. Permissions are typically specified using either
symbolic or numeric modes.
In numeric mode, permissions are represented as a three-digit octal number. Each digit
represents the permissions for the owner, group, and others, respectively.
Example,
Command: ls -l file.txt
Output: -rw-r--r-- 1 user group 1234 Aug 30 12:34 file.txt : here owner has read and write
permissions but not execute permission
GREP
The grep command in Linux is used to search for text patterns within files. It stands for "Global
Regular Expression Print" and is a powerful utility for finding lines in files that match a specified
pattern.
PIPE COMMAND
Example: ls -l | less
ls *.txt // This lists all files in the current directory that end with .txt.
ls data* // matches files like data1, data_2024, data_backup, etc.
ls a* // file names starting with a
ls *a // file names ending with a
ls ? // lists files and directories in the current directory whose names are exactly one
character long.
ls h? // matches files and directories whose names start with ‘h’ followed by exactly
one more character
For example, if your directory contains files named hi, ho, and h2, these will be listed.
Files like hello or h will not be matched because they do not conform to the h?
pattern (i.e., hello has more than one character after h, and h has no additional
character).
ls m*y // lists files and directories in the current directory whose names start with ‘m’,
followed by any number of characters, and ending with ‘y’.
Example: my, message, mystery, mighty etc.
ls [abc]* // This command lists all files and directories in the current directory whose
names start with either a, b, or c. The * allows for any characters to follow these
initial letters.
Example: If the directory contains files named apple, banana, cat, dog, and zebra, the
command will list apple, banana, and cat. It will not list dog or zebra because they do
not start with a, b, or c.
ls [a-c]* // This command lists files and directories in the current directory whose
names start with any character in the range a to c (inclusive), followed by zero or
more additional characters.
Example: If the directory contains files named apple, banana, cat, dog, and zebra, the
command will list apple, banana, and cat. Again, dog and zebra will not be listed
because they start with characters outside the specified range a-c.
PASTE
paste file1 file2: Merges lines from file1 and file2 side by side with tabs.
paste -d ',' file1 file2: Uses a comma as the delimiter.
paste file1 file2 file3: Combines lines from multiple files.
paste -: Reads from standard input.
CUT
The cut command is used to extract sections from each line of input. It can be used to cut
out sections of text from a file or standard input based on delimiters or character positions.
Syntax: cut [OPTION]... [FILE]...
Common Options:
File: data.txt
Content:
name,age,location
Alice,30,New York
Bob,25,Los Angeles
Examples:
cut -d ',' -f 1,2 data.txt // to extract the ‘name’ and ‘age’ fields
cut -c 1-5 file.txt // To extract characters 1 to 5 from each line of file.txt
TR Command
The tr command is used for translating or deleting characters. It operates on streams of text
and is often used to replace characters or remove them entirely.
cut -d ',' -f 2 file.txt | tr -d ' ' // Extract the second field and remove spaces
That is, this command first extracts the second field using cut and then pipes the result to tr to
remove all spaces.
1. Regular Files
Description: These are the most common files, containing data such as text, binaries,
images, etc.
Identification: They don’t have any special indicators. When listing files using ls -
l, they are denoted by a - at the beginning of the permissions string.
Example: document.txt, script.sh, program.bin
2. Directories
Description: Directories are containers that hold files and other directories.
Identification: Denoted by a d at the beginning of the permissions string when listing
files with ls -l.
Example: /home, /usr/bin
Description: These are references or pointers to other files or directories. They act as
shortcuts.
Identification: Denoted by an l at the beginning of the permissions string when
listing files with ls -l.
Example: link_to_file (points to target_file)
4. Character Devices
Description: These files represent devices that perform input and output operations
character by character. They are usually used for devices like terminals and serial
ports.
Identification: Denoted by a c at the beginning of the permissions string when listing
files with ls -l.
Example: /dev/tty, /dev/random
5. Block Devices
Description: These files represent devices that perform input and output operations in
blocks (chunks of data). They are used for devices like hard drives and USB drives.
Identification: Denoted by a b at the beginning of the permissions string when listing
files with ls -l.
Example: /dev/sda, /dev/mapper/cryptroot
6. Pipes (FIFOs)
Description: These files provide a way for processes to communicate with each
other. They are also known as named pipes.
Identification: Denoted by a p at the beginning of the permissions string when listing
files with ls -l.
Example: /tmp/myfifo
7. Sockets
Description: These files are used for inter-process communication (IPC) on the same
machine. They provide a way for processes to send and receive data.
Identification: Denoted by an s at the beginning of the permissions string when
listing files with ls -l.
Example: /var/run/docker.sock
8. Special Files
Description: These include various system files with special functionalities, like
/proc files that represent kernel and process information.
Identification: These files may fall under any of the categories above but are often
found in specific directories like /proc or /sys.
To see file types, you can use the ls -l command which lists files in long format, including
their types. Here’s an example:
$ ls -l
total 8
drwxr-xr-x 2 user user 4096 Aug 30 10:00 directory
-rw-r--r-- 1 user user 27 Aug 30 10:01 file.txt
lrwxrwxrwx 1 user user 11 Aug 30 10:02 symlink -> file.txt
crw-rw-rw- 1 root root 1, 3 Aug 30 10:03 tty
brw-rw---- 1 root disk 8, 0 Aug 30 10:04 sda
prw-r--r-- 1 user user 0 Aug 30 10:05 myfifo
srw-rw-rw- 1 user user 0 Aug 30 10:06 mysocket
d denotes a directory
- denotes a regular file
l denotes a symbolic link
c denotes a character device
b denotes a block device
p denotes a pipe (FIFO)
s denotes a socket
To display filenames along with their sizes in the current directory using ls, grep, tr, and cut,
you can combine these commands as follows:
ls -l: Lists files and directories with detailed information in the current directory,
including permissions, number of links, owner, group, size, modification date, and filename.
grep '^-': Filters the output to include only lines that start with -, which indicates regular
files. Lines starting with d are directories, and lines starting with other characters might be
symbolic links, device files, etc.
awk '{print $9, $5}': Extracts and prints the filename and its size. The filename is
typically the 9th field, and the size is the 5th field in the output of ls -l.
grep '^-': Filters out only the lines starting with -, which are regular files.
-rw-r--r-- 1 user group 1234 Aug 30 12:34 filename1
-rw-r--r-- 1 user group 5678 Aug 30 12:35 filename2
awk '{print $9, $5}': Extracts and displays the filename and size:
filename1 1234
filename2 5678