cat file1 file2 file3 ...
echo 'Text through stdin' | cat - file.txt
cat -s file
# Display tabs as ^I: cat -T file.py # Display line numbers: cat -n file.txt
script -t 2> timing.log -a output.session ... # type commands here ... exit
scriptreplay timing.log output.session
find base_path # For instance: find . -print # We can use -print0 to use '\0' as delimiting character. # This is usefull when flename contains spaces
find /home/slynux -name "*.txt" -print # Using the option iname to ignore case: find . -iname "example*" -print # or condition for multiple criteria: find . \( -name "*.txt" -o -name "*.pdf" \) -print # Using path argument: find /home/users -path "*/slynux/*" -print # Using regex argumen to match paths based on regular expressions: find . -regex ".*\(\.py\|\.sh\)$" # or iregex to ignore case: find . -iregex ".*\(\.py\|\.sh\)$"
# Exclude things that match a pattern: find . ! -name "*.txt" -print
# Only printing files in the current directory: find . -maxdepth 1 -name "f*" -print # Or using mindepth: find . -mindepth 2 -name "f*" -print # note that these flags should be specified as third argument for find to improve efficiency.
find . -type d -print # find directories find . -type f -print # find regular files find . -type l -print # find symlinks
# we can use the flags: -atime : access time -mtime : modification time -ctime : change time # The provided inter value is the number of days: find . -type f -atime -7 -print # all files accessed within the last 7 days find . -type f -atime 7 -print # all files accessed exactly 7 days ago. find . -type f -atime +7 -print # all files that were accessed more that 7 days ago. # we can also used minutes based flags: -amin -mmin -cmin # We can also find files newer that a given file: find . -type f -newer file.txt -print
find . -type f -size +2k # files bigger than 2KB find . -type f -size -2k # smaller than 2kB # instead of 'k', we can use 'M', 'G'
find . -type f -name "*.swp" -delete
find . -type f -perm 644 -print # For instance: find . -type f -name "*.php" ! -perm 644 -print # Or search based on user: find . -type f -user slynux -print
find . -type f -user root -exec chown slynux {} \; # In the previous command: # '{}' will be replaced by each filename. # if we want to run a command with a list of files as parameters then # we just replace ';' with '+'
find . -type f -name "*.c" -exec cat {} \;>all_c_files.txt
find . -type f -mtime +10 -name "*.txt" -exec cp {} OLD \;
find . -type f -name "*.txt" -exec printf "Text file: %s\n" {} \;
find devel/source_path \( -name ".git" -prune \) -o \( -type f -print \)
cat example.txt | xargs
cat example.txt | xargs -n 3
echo "splitXsplitXsplitXsplit" | xargs -d X
cat args.txt | xargs -n 1 ./cecho.sh # To provide n arguments we use the prototype: INPUT | xargs -n X # to provide all the arguments at once: cat args.txt | xargs ./ccat.sh
cat args.txt | xargs -I {} ./cecho.sh -p {} -l
find . -type f -name "*.txt" -print | xargs rm -f # Safer implementation is: find . -type f -name "*.txt" -print0 | xargs -0 rm -f
find source_code_dir_path -type f -name "*.c" -print0 | xargs -0 wc -l # One could also consider using the SLOCCount utility
cat files.txt | xargs -I {} cat {} # is equivalent to: cat files.txt | ( while read arg; do cat $arg; done )
echo "HELLO WHO IS THIS" | tr 'A-Z' 'a-z'
echo 12345 | tr '0-9' '9876543210' # encrypt echo 87654 | tr '9876543210' '0-9' # decrypt # ROT13 encryption: echo "tr came, tr saw, tr conquered." | tr 'a-zA-Z' 'n-za-mN-ZA-M' # decryption: echo ge pnzr, ge fnj, ge pbadhrerq. | tr 'a-zA-Z' 'n-za-mN-ZA-M'
tr '\t' ' ' < file.txt
echo "Hello 123 world 456" | tr -d '0-9'
echo hello 1 char 2 next 4 | tr -d -c '0-9 \n'
echo "GNU is not UNIX. Recursive right ?" | tr -s ' '
# Assuming sum.txt contains one number per line: cat sum.txt | echo $[ $(tr '\n' '+' ) 0 ]
tr [:class:] [:class:]
$ md5sum filename 68b329da9893e34099c7d8ad5cb9c940 filename # We can redirect the output to file: $ md5sum filename > file_sum.md5 # Prototype is: $ md5sum file1 file2 file3 ... # This will output one line per file.
$ md5sum -c file_sum.md5 # This will output a message whether checksum matches or not. # Alternatively: $ md5sum -c *.md5
$ md5deep -rl directory_path > directory.md5 # -r to enable recursive traversal # -l for using relative path. By default it writes absolute file path in output # Alternatively we can use find: $ find directory_path -type f -print0 | xargs -0 md5sum >> directory.md5
$ crypt <input_file >output_file Enter passphrase: # alternatively, we can provide the passphrase on the command line: $ crypt PASSPHRASE <input_file >encrypted_file # to decrypt: $ crypt PASSPHRASE -d <encrypted_file >output_file
$ gpg -c filename # to decrypt: $ gpg filename.gpg
$ base64 filename > outputfile # or: $ cat file | base64 > outputfile # To decode: $ base64 -d file > outputfile # or: $ cat base64_file | base64 -d > outputfile
$ opensslpasswd -1 -salt SALT_STRING PASSWORD $1$SALT_STRING$323VkWkSLHuhbt1zkSsUG.
$ sort file1.txt file2.txt > sorted.txt # or: $ sort file1.txt file2.txt -o sorted.txt # For numerical sorting: $ sort -n file.txt # To sort in reverse order: $ sort -r file.txt # To sort by month: $ sort -M months.txt # To merge 2 sorted files: $ sort -m sorted1 sorted2 # To find unique lines in sorted file: $ sort file1.txt file2.txt | uniq
#!/bin/bash #Desc: Sort sort -C filename ; if [ $? -eq 0 ]; then echo Sorted; else echo Unsorted; fi
$ cat data.txt 1 mac 2000 2 winxp 4000 3 bsd 1000 4 linux 1000 # we use the -k flag to specify the column to use: # Sort reverse by column1 $ sort -nrk 1 data.txt 4 linux 1000 3 bsd 1000 2 winxp 4000 1 mac 2000 # -nr means numeric and reverse # Sort by column 2 $ sort -k 2 data.txt 3 bsd 1000 4 linux 1000 1 mac 2000 2 winxp 4000
$ cat data.txt 1010hellothis 2189ababbba 7464dfddfdfd $ sort -nk 2,3 data.txt # To use first character as key: $ sort -nk 1,1 data.txt # To use a \0 separator: $ sort -z data.txt | xargs -0 #Zero terminator is used to make safe use with xargs # To ignore leading blank and use dictionnary order: $ sort -bd unsorted.txt
$ sort unsorted.txt | uniq
$ uniq -u sorted.txt
$ sort unsorted.txt | uniq -c 1 bash 1 foss 2 hack
$ sort unsorted.txt | uniq -d hack
$ cat data.txt u:01:gnu d:04:linux u:01:bash u:01:hack $ sort data.txt | uniq -s 2 -w 2 d:04:linux u:01:bash
$ uniq -z file.txt
$ filename=`mktemp` $ echo $filename /tmp/tmp.8xvhkjF5fH
$ dirname=`mktemp -d` $ echo $dirname tmp.NI8xzW7VRX
$ tmpfile=`mktemp -u` $ echo $tmpfile /tmp/tmp.RsGmilRpcT
$mktemp test.XXX test.2tc
$ split -b 10k data.file $ ls data.file xaa xab xac xad xae xaf xag xah xai xaj
$ split -b 10k data.file -d -a 4
$ split -b 10k data.file -d -a 4 split_file
$ split -l 10 data.file
csplit server.log /SERVER/ -n 2 -s {*} -f server -b "%02d.log" ; rm server00.log
file_jpg="sample.jpg" name=${file_jpg%.*} echo File name is: $name
extension=${file_jpg#*.}
$ VAR=hack.fun.book.txt $ echo ${VAR%.*} hack.fun.book $ echo ${VAR%%.*} hack
$ VAR=hack.fun.book.txt $ echo ${VAR#*.} fun.book.txt $ echo ${VAR##*.} txt
#!/bin/bash #Filename: rename.sh #Desc: Rename jpg and png files count=1; for img in `find . -iname '*.png' -o -iname '*.jpg' -type f -maxdepth 1` do new=image-$count.${img##*.} echo "Renaming $img to $new" mv "$img" "$new" let count++ done
$ rename *.JPG *.jpg
$ rename 's/ /_/g' *
$ rename 'y/A-Z/a-z/' * $ rename 'y/a-z/A-Z/' *
$ find path -type f -name "*.mp3" -exec mv {} target_dir \;
$ find path -type f -exec rename 's/ /_/g' {} \;
#!/bin/bash #Filename: checkword.sh word=$1 grep "^$1$" /usr/share/dict/british-english -q if [ $? -eq 0 ]; then echo $word is a dictionary word; else echo $word is not a dictionary word; fi # Usage as: $ ./checkword.sh ful ful is not a dictionary word $ ./checkword.sh fool fool is a dictionary word
$ look word filepath # or: $ grep "^word" filepath
$ echo -e "1\nhello\n" | ./interactive.sh You have entered 1, hello # -e flag for echo means 'interpret escape sequences'
#/bin/bash #filename: generate_checksums.sh PIDARRAY=() for file in File1.iso File2.iso do md5sum $file & PIDARRAY+=("$!") # $! : retrieves the PID of the last background process. done wait ${PIDARRAY[@]}