Posts

Showing posts from February, 2024

If Conditons for a | FILE related

  In shell scripting, various file-related conditions can be used to check the properties of files. Here are some common file-related conditions in Bash scripting: Existence Checks : -e file : True if file exists. -f file : True if file exists and is a regular file. -d file : True if file exists and is a directory. -s file : True if file exists and has a size greater than zero. Permission Checks : -r file : True if file exists and is readable. -w file : True if file exists and is writable. -x file : True if file exists and is executable. Ownership Checks : -O file : True if file exists and is owned by the current user. -G file : True if file exists and is owned by the current group. Modification Time Checks : -nt file : True if the current file is newer than file . -ot file : True if the current file is older than file . Type Checks : -h file : True if file exists and is a symbolic link. -L file : Same as -h . -p file : True if file exists and is a named pipe (FIFO). -S ...

Compare Files | CMP | COMM | DIFF

Image
Compare Files : Command is "cmp"  -- compares byte by byte comparing two files byte by byte and it will show the first different byte. cmp <file1.txt> <file2.txt>  Compare only given bytes  cmp -n 100 <file1.txt> <file2.txt>   to compare only given number of bytes use flag "-n" Verbose command "-l" cmp -l file1.txt file2.txt The bytes started changed from 202 and then eac byte changes. Print only different Bytes   -- use -b flag cmp -b file1.txt file2.txt COMM command Comm compares two sorted files line by line. Display only common data between the two files comm -1 -2 file1.txt file2.txt   // -1 remove col 1 , -2 remove column 2 and display only the common column DIFF COMMANDS COMM command is also used to compare line by line. Here we get three columns -- Data that is different in file 1 and Data that is different in file 2 and common data DIFF : Diff command only displays different lines diff file1.txt file2.txt -q f...

GREP Command

Image
Grep Command GREP --  stands for Global Regular Expression Parser searching for a word in a file grep 200 filename example I am searching for string "ravana" in the file $ grep ravana data.txt Ravana  9309080497      ravana@gmail.com        Newzealand Ravana  9309080497      ravana@gmail.com        Newzealand You can also use "" to look for the content $ grep "ravana" data.txt Ravana  9309080497      ravana@gmail.com        Newzealand Ravana  9309080497      ravana@gmail.com        Newzealand Ravana  9309080497      ravana@gmail.com        Newzealand Searching data in more than one file. grep "john" filename1 filename2 $ grep "ravana" data.txt info.txt data.txt:R...

More & Less Command

Image
More command more <file_name> if you want to next page press space to come out of the file -- press q more -d <file_name> -d flag shows the  but with more command you can only move toward forward direction. Less command less <file_name>     type "d" for one page down type "b" for back one page back Less command with line numbers less -N filename

Tail Command

Image
Display the last 10 lines from a file. tail -n 10 <file_name> by default the tail command displays last 10 lines. tail <file_name>

Head Command

Image
head command by default displays the first 10 lines of a file. display 20 lines in a file  head -n 20 <file_name> or you can also use the below head -20 <file_name> Display 200 bytes of data from the file head -c 200 <file_name> Skill last 28 lines  // it only skips the last 28 lines head -n -28 <file_name> -- Head command to fetch lines - particular lines using pipe "|" symbol head -n 23 movies.txt | tail -n 8  -- now we want to display only those movies that has 2009 as their year head -n 23 movies.txt | tail -n 8 | grep 2009 $ head -n 10 movies.txt | grep 2010 Search in process result // this will display only those process that have the keywork monitor in them ps -ef | grep monitor    Display only specific file or folder ls -l | grep -i Folder   // -i says ignore case -- non case senstive.

CAT Command

  CAT : Commands Displaying the lines numbers of each line in a file. cat -n <file_name>   -- this will display customer data. creating a file using cat  Creating a file cat > file_name.txt --copy paste the contents of the file here. and control + c to come out and the file is created  Merge multiple file and create a new file  cat <file_name_1> <file_name_2  >   new_file.txt Copy content of  a file into a new file in append mode cat <file_name_1> <file_name_2>  >> newfileappended.txt showing the file in reverse order - command is reverse of "cat" > "tac" Line numbers in front of each line . cat -n <file_name> Skip blank lines from a file use -b flag cat  -n -b  <file_name> check a file in reverse order  tac <file_name>