Prefix The Output of Any Command with a Timestamp
You can prefix the output of every line with a timestamp by piping the command to the following sed command:
$ command | sed "s/^/\[
date +“%Y%m%d%H%M%S”]/"
Some sed Tips
Put an * between the number 5 and 6
$ echo '12345678901234567890'| sed -e 's/^.\{5\}/&*/'
12345*678901234567890
Put a comma between each number
$ echo "1234567890" |sed -e 's/./&,/g' -e 's/,$//'
1,2,3,4,5,6,7,8,9,0
Put comma after every 2, if not even then last number exist by itself
$ echo "1234567890" |sed -e 's/../&,/g' -e 's/,$//'
12,34,56,78,90
Double space the data in file data1
$ sed G data1
Triple space the data file file data1
$ sed 'G;G' data1
Single space a double spaced file
$ sed 'n;d' data1
DOS to Linux conversion
$ sed 's/.$//' DOSfile >Linuxfile
Truncate everything after a substring (‘with’ in this case):
$ cat file.txt
Brown Potatoes with Cheese
Yellow Potatoes with Sugar and Cheese
$ sed -i.bak 's/with.*//g' file.txt
$ cat file.txt
Brown Potatoes
Yellow Potatoes
Archive Only Files In a Directory
If you want to create a tar archive of only the files of a directory and exclude any subdirectories you can use the ls -la
command and pipe the output to awk
. However you need to remove the first 8 fields from the output and leave all of the remaining parts of the line in case there are spaces in the filename. One quick and dirty way of doing that is to set each of the 8 fields to a blank and then use sed
to trim the leading spaces. You can optionally add quotation marks around the filename in your output too.
$ ls -al | awk '$0!~/^d/ {$1=$2=$3=$4=$5=$6=$7=$8=""; printf("%s\"\n", $0)}' | sed 's/^[[:space:]]*/"/' | xargs tar cvf archive-name.tar
Tags: cli, tar, awk, xargs, sed, motd
How To Count All The Files Extension Recursively In Linux
To count all the files by file extension recursively on the command line
$ find . -type f | sed -n 's/..*.//p' | sort | uniq -c
40 3g2
5 AVI
13 DS_Store
28 JPG
30 MOV
133 MP4
64 THM
1 docx
18 jpg
1 json
4 m3u
89 m4a
2 m4r
156 m4v
41 mkv
112 mov
38 mp3
587 mp4
1 nfo
2 osp
30 png
1 sh
4 srt
6 svg
10 torrent
6 txt
5 webm
10 zip