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 file.txt // 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 file.txt # Remove a file
rm -r directory/ # Remove a directory and its contents recursively
rm -f file.txt # Forcefully remove a file without prompting
cp source.txt destination.txt # Copy a file
cp -r source_directory/ destination/ # Copy a directory and its contents
mv oldname.txt newname.txt # Rename a file
mv file.txt /path/to/destination/ # Move a file to a new location
touch newfile.txt # Create an empty file or update the timestamp of an existing file.
echo "Hello, World!" > hello.txt // write text to a file (overwrites the file if it exists)
cat > newfile.txt // Create a new file by entering content manually (use Ctrl+D to
finish and save input).
cat file.txt # Display the contents of a file
cat file1.txt file2.txt # Display contents of multiple files
cat >>file.txt // append content to the file
paste file1 file2 // merges corresponding lines of files side by side
more file.txt # View the contents of a file page by page
less file.txt # View the contents of a file with more features than 'more'
head file.txt # Display the first 10 lines of a file
head -n 20 file.txt # Display the first 20 lines of a file
tail file.txt # Display the last 10 lines of a file
tail -n 20 file.txt # Display the last 20 lines of a file
tail -f file.txt # Continuously monitor and display new lines added to a file
cp source.txt destination.txt // copy files or directories
mv oldname.txt newname.txt// move or rename files or directories
rm file.txt// remove files or directories
chmod 755 file.txt # Set permissions to rwxr-xr-x
chmod u+x file.txt # Add execute permission for the owner
chmod u-w file.txt # Remove write permission for the owner
chown user:group file.txt # Change owner and group
chown user file.txt # Change only owner
chgrp group file.txt # 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 "filename.txt" # Find file by name
grep "search text" file.txt # Search for 'search text' in file.txt
diff file1.txt file2.txt // 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 file.txt sets the permissions of file.txt 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 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
Add execute permission,
Command: chmod u+x file.txt
Command: ls -l file.txt
Output: -rwxr--r-- 1 user group 1234 Aug 30 12:34 file.txt
Now, file.txt 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" file.txt // search for a specific string in a file
Example: grep “error” log.txt // searches string “error” in the file log.txt and prints all lines
containing “error”
To search regardless of case,
grep –i “error” log.txt // -i used to search in case insensitive manner
To search whole words only,
grep -w "error" log.txt // This searches for "error" as a whole word and will not match "errors" or
"preerror" in log.txt.
Basic Search: grep "pattern" file.txt
Case-Insensitive: grep -i "pattern" file.txt
Whole Words: grep -w "pattern" file.txt
Multiple Patterns: grep -e "pattern1" -e "pattern2" file.txt // Search for
multiple patters at once.
Example: grep -e "error" -e "warning" log.txt
Line Numbers: grep -n "pattern" file.txt
Context Lines: grep -B num "pattern" file.txt (before), grep -A num "pattern"
file.txt (after), grep -C num "pattern" file.txt (both)
Invert Match: grep -v "pattern" file.txt
Recursive Search: grep -r "pattern" directory/
Multiple Files: grep "pattern" file1.txt file2.txt
Regular Expressions: grep -E "regex" file.txt
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: 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.
tr 'a' '@' < file.txt // To replace all occurrences of a with @ in a file
tr -d ' ' < file.txt // To delete all spaces from the input
tr 'a-z' 'A-Z' < file.txt // To convert all lowercase letters to uppercase
Combining cut and tr,
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
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/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.
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 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 | 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