Text Handling and File Management in Linux
Managing texts and file operations using Linux commands
๐ Hello!
In this article, I will be covering all the necessary commands that will be required in managing files and text handling in Linux. Searching texts in files, searching specific folders and files require lots of time if one is not familiar with important commands. I will be creating a detailed article on this along with helpful multiple options you can use to perform text and file operations.
Here are the most useful commands,
Commands for searching files/directories and texts in Linux
1. ๐ Find Command
Syntax | Description |
find [path] -name | to find file in specific directory |
find [path] -iname "*.cpp" | find ignoring cases file with extension .cpp |
find [path] -maxdepth 2 -iname ".cpp" | to get in 2nd directory i.e maxdepth |
find [path] -maxdepth 2 not -iname ".cpp" | negation of above command |
find [path] -maxdepth 2 not -iname ".cpp" -o -iname ".h" | or include file with .h [-o = or] |
find [path] -maxdepth 2 not -iname ".cpp" -a -iname ".h" | or include file with .h [-a = and] |
find [path] -type f -name "abc" | to find file [regular file = f] |
find [path] -type d -name "abc" | to find directory with name abc |
find [path] /hom/path -type f -name "abc" | to find in two path |
find [path] -type f -name ".*" | to find hidden files |
find [path] -type d -perm 777 | find all the directories with that permission |
find [path] -type f ! -perm 0777 | files with not permission 0777 |
find [path] -perm /u=r | u = user r = read permission |
find [path] -atime 10 | list all file access in last 10day |
find [path] -size 50M | file size of 50 mib |
find [path] -size +50M -size -100M | |
find [path] -type f -empty | to find empty file, for directory use d |
find [path] -user apache | find directories/files owned by user apache |
find [path] -group apache | find directories/files owned by group apache |
find [path] -atime +10 -mtime -100 | list all file [m = modified] access in between 10-100 days |
find [path] -cmin 10 | in last 10 min [c = change of status] |
Sources doc
2. ๐ Locate Command
locate - find files by name
- locate has its own database that updates to store details of files.
- It has a cron job running to perform the update in the database.
- locate is faster than find command.
- In most of the Linux distro it is not pre-installed, installation is required to use.
locate [OPTION] PATTERN
Sources doc
3. โ๏ธ Grep Command
grep command is used to search texts, here are a few usages that will be helpful.
Syntax | Description |
grep [options] "pattern" | pattern matching |
grep -rih "pattern" filename | h = to remove filename and show content |
grep -riw "pattern" filename | to match exact pattern, r recursively |
grep -E 'pattern1 | pattern2' filename | to find both pattern (extended grep) |
grep -c 'pattern' filename | to count number of matched pattern |
grep -l 'pattern' filename | to get list of file containing pattern |
grep '[0-9] [0-9] [0-9]' filename | finding pattern with range limit |
grep -E 'pattern1 | pattern2' --color=always | to highlight matched pattern |
grep -i -n -B 2 -A 3 pattern filename | -B is to show number of lines before the matching lines, -A show the lines after matching |
grep -v 'pattern' filename | to show non matched pattern lines |
Sources doc
Commands for handling texts and Files
1. Cat - Concatenate Command ๐ฑ
Syntax | Description |
cat [options] filename | to see content of file |
cat -n filename | to see line numbers |
cat filename1 filename2 >mergefilename | to merge two files content |
For more options refer to doc
2. rm- Remove Command ๐ด๓ ฃ๓ ฌ๓ ฒ๓ ญ๓ ฟ
Syntax | Description |
rm [options] filename | to remove file |
rm -f filename | to remove file forcefully |
rm -rf directory_name or filename | to recursively delete directory with forcefully |
See more options doc
3. cp- copy Command โ๏ธ
Syntax | Description |
cp [options] filename or directoryname | to copy file or directory |
cp -rf directoryname | to copy directory |
See more options doc
4. head Command โก
To display top n lines
head -n <no_of_lines_to_display> <filename>
See more options doc
5. tail Command ๐
To display top n lines
Syntax | Description |
tail -n no_of_lines_to_display filename | to display last n lines |
tail -f filename | to see the files growing in real-time |
tail -v filename1 filename2 | to see filename on console as well with real-time data multiple files are options we can use single file also |
See more options doc
6. Less/More Command ๐ณ๏ธ
To control file display on the command line
more/less filename
For controlling display options see doc
7. diff - Difference Command ๐
Compare the first file with the second file to see what changes are to be made in the first file to make it like the second file.
Comparison result returns 3 characters code, the first character is the line number of the first file, the second character is the type of change, the third character is the line number in the second file.
- โ>โ: this symbol means line to be added in the first file
- โ<โ: this symbol means line to be changed or removed in the first file
- a, c, d: symbol means added, changed, deleted
Example characters code:
- 7a8: after line 7 in the first file add the contents of line 8 from the second file
- 1d0: In the first file delete line 1 so that both files have a common start at 0
- 7c7: Change line 7 of the first file to make it the same as the line7 of the second file
Syntax | Description |
diff file1 file2 | to check difference between two files |
diff -c file1 file2 | Compare two files line by line compact way |
diff --color=always file1 file2 | to see changes in color format |
diff -y file1 file2 | Compare two files result side by side |
diff -u file1 file2 | unified way comparison |
diff -q file1 file2 | to check file is changed or not |
diff -i file1 file2 | case insensitive match |
diff -B file1 file2 | to ignore blank line |
diff -B -s file1 file2 | to ignore blank line and check file are same |
diff -w -s file1 file2 | to ignore white spaces and check file are same |
diff -r directory1 directory2 | to compare two directories |
Fore more options refer doc
8. wc - word count command ๐งฎ
Syntax | Description |
wc filename | result displays the number of lines, number of words, number of characters, filename |
wc -m filename | to get the total number of characters only |
wc -w filename | to get the total number of words only |
wc -l filename | to get the total number of lines only |
wc -L filename | to get the length of longest line |
wc -c filename | to get the total number of bytes |
9. tr - Translate command
- Used to translate and deleting the characters
- Transform or format text characters
Syntax | Description |
tr [:lower:] [:upper:] | convert lower case to upper case characters |
tr \t , | to separate tab-delimited values with comma |
tr -s \n | replacing blank line |
Fore more options refer doc
10. Sort Command ใฝ๏ธ
Syntax | Description |
sort [filename] | to sort in ascending order |
sort -u [filename] | to remove duplicate values |
sort -n [filename] | to sort the numerical values |
sort -r [filename] | sort in descending order |
sort -k 1,2 [filename] | sort by column order, sort by 1st column then by 2nd |
sort -o [new_filename] | output to file |
sort -n -k 2 -t โ,โ [filename] | sort the -n numeric type -k column number, -t delimiter type |
sort -c [filename] | to check file is sorted or not |
Fore more options refer doc
11. Uniq Command โ๏ธ
uniq command is used to remove duplicate values that come together
Syntax | Description |
uniq -i filename | ignore case-sensitiveity |
uniq -i -c filename | to see count of occurecences |
uniq -i -d filename | to see repeated lines |
uniq -i -u filename | to see non repeated lines |
Fore more options refer doc
12. Cut Command โ๏ธ
To cut a particular section in file
cut -d โ:โ -f 1 <filename> //to cut particular column in file with specific delimiter
Fore more options refer doc
Note: Combination of commands are powerful using pipe operation
cut -d โ:โ -f 1 <filename> | sort | uniq | tr โ[a-z]โ โ[A-Z]โ
Thank you for reading!