Gnu/Linux command line "one-liners"
Most probably you are pondering how to solve a specific problem using the command line. The following solutions are specific cases that I encountered; it is most likely that you have found similar solutions. The purpose of this repository is to represent an on-line substitution of my paper-notebook of one-liners.
-
Reverse the content of all specified text files using
findandvicommandsfind . -iname '*.DPT' -exec vi '{}' -c ':g/^/m0' -c ':wq' \;
-
Copy one file into all sub folders in the current working directory
find . -type d -exec cp file.name '{}' \;
-
swap a file using
find,cpandrename(in two steps) in case two files are in separate folders or sub folders. The 'old.f' content and filename is swapped to 'new.f'.find . -iname '*.f' -exec cp ./path-to-file/new.f '{}' \;find . -iname 'new.f' -exec rename 's/old.f/new.f/' '{}' \;
-
search files in-between specific date
-
using
find-
find . -newermt '2015-03-03' ! -newermt '2015-03-04' -
find . -newermt 'Nov 11 03:56' ! -newermt 'Nov 11 03:59' -printf '%Tc %p\n'where
-printf '%Tc %p\n'will print out the exact dateto be more specific use:
-printf '%TY-%Tm-%Td--%TH:%TM:%TS'%pprints out the pathadding
| sort -nto the end will do the evident sorting
-
-
using
lsandawkls -ltr | grep 'Nov.*5.*10' | awk '{print $NF}'
and copy them to another folder:
ls -ltr | grep 'Nov.*5.*10' | awk '{print $NF}' | xargs -i cp '{}' folder
-
-
returning path string with find:
-
find `pwd` ....will return the absolute path, since find is giving output relative to the input -
find . ..... -
find . -iname '*x' -printf '%p\n'will print out the filename only without the path
-
-
find with subfolder depth control
find . -maxdepth 1 -iname '*.py*
-
find partial path and file
find -path '*.git/logs/HEAD'usinginameinstead ofpathdoes not work
-
excluding folders from find
find . -path ./some/path -prune -o -iname '*some*' -print
or without
-prunefind . -iname '*some*' -not -path "./some_path/*"
the
*is very much needed, otherwise it will exclude only the given path and not all paths below- in addition one can exclude a given folder at any level by
find . -iname '*some*' -not path "*/some_path/*"
-
combine find file by name and path
find . -iname "*.pbz2 -path "./2022*"
-
combining regular expression with
find. For example find all filenames that are longer then 5 charactersfind -regextype posix-egrep -regex '.*[^/]{5}'
-
multiple find ; to use logical operators with
findthrough regular expressionfind . -regextype posix-egrep -regex "(.*bgg.*\.*add.*)"ANDfind . -regextype posix-egrep -regex "(.*bgg.*|.*add.*)"OR
-
chaining commands inside
find, for example rename specific files and move them to another folderfind . -iname '*.dat' -exec rename 's/dat/DPT/' '{}' \; -exec mv '{}' ~/home/somewhere \;
-
remove files but skip some
rm !(*.zip)orrm !(*.zip|*.dat)rm -r */to remove only folders
-
cleaning git repo
find . -name "*.pyc" -exec git rm {} \;
-
sorting using
sort-
sort -k 2,2n -k 3 file.txtwhere
-k 2,2n -k 3means to sort data using the given column number. First, it will sort 2nd column (date dd field) and then 3rd column (time). Found here
-
-
a compilation of sed one-liners
-
networking using
nmcli- Connect to netwrok
nmcli device wifi connect <SSID> password <password> - connect to netwrok
nmcli con up id <SSID> - check available devices
nmcli d - check all wifi networks in the area with signal strength
nmcli d wifi
- Connect to netwrok
du,dfcommands to investigate storage space:du -sh ./* | sort -hwatch -n 1 "df -h | grep "Filesystem" && df -h | grep '/$' && df | grep '/$'"du -sh .[^.]*- for inspection of hidden folders onlydu -sh --exclude=/media /- to exclude a folder from the calcuation of folder sizes
screencommand add to./.screenrc:termcapinfo xterm* ti@:te@. For logging the std output of screen funciton into a file us the-Lflag:screen -L -S NAME_OF_SESSION
The fact you can do something doesn't necessarily mean you should! ;)
+++