LINUX
ls # List files and directories in the current directory
ls -l # List in long format with permissions, ownership, size, and modification date
ls -a # List all files, including hidden files
ls -lh # List in long format with human-readable file sizes
stat [Link] // Display detailed information about a file.
cd /path/to/directory # Change to the specified directory
cd .. # Move up one directory
cd ~ # Go to the home directory
pwd # Display the full path of the current directory
mkdir new_directory # Create a new directory
mkdir -p parent/child # Create a parent directory and child directory in one
command
rmdir empty_directory # Remove an empty directory
rm [Link] # Remove a file
rm -r directory/ # Remove a directory and its contents recursively
rm -f [Link] # Forcefully remove a file without prompting
cp [Link] [Link] # Copy a file
cp -r source_directory/ destination/ # Copy a directory and its contents
mv [Link] [Link] # Rename a file
mv [Link] /path/to/destination/ # Move a file to a new location
touch [Link] # Create an empty file or update the timestamp of an existing file.
echo "Hello, World!" > [Link] // write text to a file (overwrites the file if it exists)
cat > [Link] // Create a new file by entering content manually (use Ctrl+D to
finish and save input).
cat [Link] # Display the contents of a file
cat [Link] [Link] # Display contents of multiple files
cat >>[Link] // append content to the file
paste file1 file2 // merges corresponding lines of files side by side
more [Link] # View the contents of a file page by page
less [Link] # View the contents of a file with more features than 'more'
head [Link] # Display the first 10 lines of a file
head -n 20 [Link] # Display the first 20 lines of a file
tail [Link] # Display the last 10 lines of a file
tail -n 20 [Link] # Display the last 20 lines of a file
tail -f [Link] # Continuously monitor and display new lines added to a file
cp [Link] [Link] // copy files or directories
mv [Link] [Link]// move or rename files or directories
rm [Link]// remove files or directories
chmod 755 [Link] # Set permissions to rwxr-xr-x
chmod u+x [Link] # Add execute permission for the owner
chmod u-w [Link] # Remove write permission for the owner
chown user:group [Link] # Change owner and group
chown user [Link] # Change only owner
chgrp group [Link] # Change group of the file
df # Display disk space usage of all mounted file systems
df -h # Display disk space usage in human-readable format (e.g., GB, MB)
echo "Hello, World!" # Print "Hello, World!" to the terminal
man ls # Display the manual page for the `ls` command
history # Show a list of previously executed commands
find /path/to/search -name "[Link]" # Find file by name
grep "search text" [Link] # Search for 'search text' in [Link]
diff [Link] [Link] // compare files line by line
command1 | command2 // pipe command ‘|’ is used to pass the output of one
command as input to another command
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.
chmod 755 [Link] sets the permissions of [Link] to rwxr-xr-x.
Here's a breakdown of what 755 means:
7 (for the owner): rwx (read, write, and execute)
5 (for the group): r-x (read and execute)
5 (for others): r-x (read and execute)
Example,
Command: ls -l [Link]
Output: -rw-r--r-- 1 user group 1234 Aug 30 12:34 [Link] : here owner has read and write
permissions but not execute permission
Add execute permission,
Command: chmod u+x [Link]
Command: ls -l [Link]
Output: -rwxr--r-- 1 user group 1234 Aug 30 12:34 [Link]
Now, [Link] has rwxr--r-- permissions. This means:
Owner (user): Can read, write, and execute.
Group: Can read.
Others: Can read.
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.
Basic syntax: grep [options] pattern [file...]
pattern: The text or regular expression you want to search for.
file: The file(s) to search within. If no file is specified, grep reads from standard input.
Examples,
Syntax: grep "search_term" [Link] // search for a specific string in a file
Example: grep “error” [Link] // searches string “error” in the file [Link] and prints all lines
containing “error”
To search regardless of case,
grep –i “error” [Link] // -i used to search in case insensitive manner
To search whole words only,
grep -w "error" [Link] // This searches for "error" as a whole word and will not match "errors" or
"preerror" in [Link].
Basic Search: grep "pattern" [Link]
Case-Insensitive: grep -i "pattern" [Link]
Whole Words: grep -w "pattern" [Link]
Multiple Patterns: grep -e "pattern1" -e "pattern2" [Link] // Search for
multiple patters at once.
Example: grep -e "error" -e "warning" [Link]
Line Numbers: grep -n "pattern" [Link]
Context Lines: grep -B num "pattern" [Link] (before), grep -A num "pattern"
[Link] (after), grep -C num "pattern" [Link] (both)
Invert Match: grep -v "pattern" [Link]
Recursive Search: grep -r "pattern" directory/
Multiple Files: grep "pattern" [Link] [Link]
Regular Expressions: grep -E "regex" [Link]
PIPE COMMAND
Example: ls -l | less
ls -l: Lists files in long format.
less: Allows you to scroll through the output page by page.
SHELL WILD CARD CHARACTERS
‘*’- Matches zero or more characters in a file name or text
Example:
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.
ls [c-a]* // invalid command. Ranges in brackets must be specified in ascending
order. Most shells will either ignore this pattern or show an error, depending on their
behaviour.
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:
-b : Select only these bytes.
-c : Select only these characters.
-d : Specify a delimiter (default is TAB).
-f : Select only these fields, using the delimiter specified by -d.
Consider,
File: [Link]
Content:
name,age,location
Alice,30,New York
Bob,25,Los Angeles
Examples:
cut -d ',' -f 1,2 [Link] // to extract the ‘name’ and ‘age’ fields
cut -c 1-5 [Link] // To extract characters 1 to 5 from each line of [Link]
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.
tr 'a' '@' < [Link] // To replace all occurrences of a with @ in a file
tr -d ' ' < [Link] // To delete all spaces from the input
tr 'a-z' 'A-Z' < [Link] // To convert all lowercase letters to uppercase
Combining cut and tr,
cut -d ',' -f 2 [Link] | 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: [Link], [Link], [Link]
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
3. Symbolic Links (Symlinks)
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/[Link]
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.
Examples Using Commands
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 [Link]
lrwxrwxrwx 1 user user 11 Aug 30 10:02 symlink -> [Link]
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 | grep '^-' | awk '{print $9, $5}'
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.
ls -l: Produces a long listing format.
-rw-r--r-- 1 user group 1234 Aug 30 12:34 filename1
-rw-r--r-- 1 user group 5678 Aug 30 12:35 filename2
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