Text Handling and File Management in Linux

Managing texts and file operations using Linux commands

ยท

8 min read

๐Ÿ‘‹ 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

SyntaxDescription
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 -emptyto find empty file, for directory use d
find [path] -user apachefind directories/files owned by user apache
find [path] -group apachefind directories/files owned by group apache
find [path] -atime +10 -mtime -100list all file [m = modified] access in between 10-100 days
find [path] -cmin 10in 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.

SyntaxDescription
grep [options] "pattern"pattern matching
grep -rih "pattern" filenameh = to remove filename and show content
grep -riw "pattern" filenameto match exact pattern, r recursively
grep -E 'pattern1 | pattern2' filenameto find both pattern (extended grep)
grep -c 'pattern' filenameto count number of matched pattern
grep -l 'pattern' filenameto get list of file containing pattern
grep '[0-9] [0-9] [0-9]' filenamefinding 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' filenameto show non matched pattern lines

image.png image.png image.png

Sources doc

Commands for handling texts and Files

1. Cat - Concatenate Command ๐Ÿฑ

SyntaxDescription
cat [options] filenameto see content of file
cat -n filenameto see line numbers
cat filename1 filename2 >mergefilenameto merge two files content

image.png image.png image.png

For more options refer to doc

2. rm- Remove Command ๐Ÿด๓ ฃ๓ ฌ๓ ฒ๓ ญ๓ ฟ

SyntaxDescription
rm [options] filenameto remove file
rm -f filenameto remove file forcefully
rm -rf directory_name or filenameto recursively delete directory with forcefully

image.png

See more options doc

3. cp- copy Command โœ‰๏ธ

SyntaxDescription
cp [options] filename or directorynameto copy file or directory
cp -rf directorynameto copy directory

image.png

See more options doc

4. head Command โšก

To display top n lines

head -n <no_of_lines_to_display> <filename>

image.png

See more options doc

5. tail Command ๐Ÿ“ƒ

To display top n lines

SyntaxDescription
tail -n no_of_lines_to_display filenameto display last n lines
tail -f filenameto see the files growing in real-time
tail -v filename1 filename2to see filename on console as well with real-time data multiple files are options we can use single file also

image.png image.png image.png

See more options doc

6. Less/More Command ๐Ÿณ๏ธ

To control file display on the command line

 more/less filename

image.png

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
SyntaxDescription
diff file1 file2to check difference between two files
diff -c file1 file2Compare two files line by line compact way
diff --color=always file1 file2to see changes in color format
diff -y file1 file2Compare two files result side by side
diff -u file1 file2unified way comparison
diff -q file1 file2to check file is changed or not
diff -i file1 file2case insensitive match
diff -B file1 file2to ignore blank line
diff -B -s file1 file2to ignore blank line and check file are same
diff -w -s file1 file2to ignore white spaces and check file are same
diff -r directory1 directory2to compare two directories

Fore more options refer doc

8. wc - word count command ๐Ÿงฎ

SyntaxDescription
wc filenameresult displays the number of lines, number of words, number of characters, filename
wc -m filenameto get the total number of characters only
wc -w filenameto get the total number of words only
wc -l filenameto get the total number of lines only
wc -L filenameto get the length of longest line
wc -c filenameto get the total number of bytes

image.png image.png image.png image.png image.png image.png

9. tr - Translate command

  • Used to translate and deleting the characters
  • Transform or format text characters
SyntaxDescription
tr [:lower:] [:upper:]convert lower case to upper case characters
tr \t , to separate tab-delimited values with comma
tr -s \nreplacing blank line

image.png

Fore more options refer doc

10. Sort Command ใ€ฝ๏ธ

SyntaxDescription
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

image.png image.png image.png image.png image.png image.png

Fore more options refer doc

11. Uniq Command โ„๏ธ

uniq command is used to remove duplicate values that come together

SyntaxDescription
uniq -i filenameignore case-sensitiveity
uniq -i -c filenameto see count of occurecences
uniq -i -d filenameto see repeated lines
uniq -i -u filenameto see non repeated lines

image.png image.png

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

image.png

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]โ€™

image.png

Thank you for reading!

ย