The best UNIX command for files

Unix file commands
Unix file commands

The question I have found in one recent project, that how to get top 2 duplicate records.

Sort file1 |uniqued | head2

What this command does…

  • Sorts File
  • Display duplicate records
  • Top 2 duplicate records

The list of total UNIX commands

  • cat – display or concatenate files
  • cd – change directory
  • chmod – change the permissions on a file or directory
  • compress – compress a file
  • cp – copy a file
  • date – display the current date and time
  • diff – display differences between text files
  • echo – echo arguments to the standard
  • output file – determine the type of a file
  • find – find files of a specified name or type
  • finger – display information about a user
  • ftp – file transfer program
  • grep – searches files for a specified string or expression
  • kill – kill a process
  • lpr – print out a file l
  • s – list names of files in a directory
  • man – display an on-line manual page
  • mkdir – make a directory
  • more – scan through a text file page by page
  • mv – move or rename files or directories
  • nice – change the priority at which a job is being run
  • passwd – change your password ps – list processes
  • pwd – display the name of your current directory
  • quota – disk quota and usage
  • rm – remove files or directories
  • rmdir – remove a directory
  • sort – sort and collate lines
  • talk – talk to another user
  • wc – display a count of lines, words and characters

The detailed line by line explanation for UNIX scripts from simple to complex.The detailed line by line explanation for UNIX scripts from simple to complex.

#!/bin/bash

Counting the number of lines in a list of files

for loop over arguments

if [ $# -lt 1 ]
then
echo “Usage: $0 file …”
exit 1
fi

echo “$0 counts the lines of code”
l=0
n=0
s=0
for f in $*
do
l=wc -l $f | sed 's/^\([0-9]*\).*$/\1/'
echo “$f: $l”
n=$[ $n + 1 ]
s=$[ $s + $l ]
done

echo “$n files in total, with $s lines in total”

Advertisements

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.