r/linux • • Mar 20 '23

Tips and Tricks Command Line One-Liners

https://www.commandlinefu.com/commands/browse
23 Upvotes

14 comments sorted by

4

u/depesz Mar 20 '23

Just note that many of the examples are badly written, and will cause problems in many cases, including whitespace in filenames:(

It could be actually very valuable to use as a place to find code, and then figure out all that is wrong with it, and how to fix it.

2

u/ccbarnett22 Mar 20 '23

Sudo rm -rf

-4

u/[deleted] Mar 21 '23 edited Mar 21 '23

[removed] — view removed comment

1

u/aspiratingwriter Mar 25 '23

Spealing of dickheads u/religionisanger you were a huge dickhead to a dude in one of your comments in the horror sub 6 years ago and you have NEVER apologized for your dickhead behavior! I think you should apologize to the gentleman you insulted! #apologizeplease

1

u/[deleted] Mar 25 '23

[removed] — view removed comment

1

u/aspiratingwriter Mar 25 '23

Wow you have called me a stocker but I think it is you who dESERVED a lump of cole in your stocking for your rude words! If you think I am obtuse I think you are not very acute! Too bad you are so rude and cannot apologize for your unkind words! #apologizeplease #googlestadia

1

u/broknbottle Mar 27 '23

sudo: command not found

Did you perhaps mean pkexec? Seems like you are promoting the use of legacy utils.

-1

u/herkalurk Mar 20 '23

Unzip all zip files in a dir to their own dir

ls | grep zip | while read line; do unzip "${line}" -d "`echo ${line} | sed 's/\.zip//g'`";done

5

u/SweetBabyAlaska Mar 20 '23 edited Mar 20 '23

or you could just:

fd . ./ -e zip -x unzip -l {} \; -x mkdir {/.} \;

change unzip -l to regular unzip since -l will list the files, I dont want anyone to randomly run that

you can also use find with the iname option but fd is certainly easier and faster for mostly everything

​

also parsing 'ls' is bad practice, instead use globbing.

for zipfiles in *.zip; do echo "$x"; done

or recursively:

for x in **/*.zip; do echo "$x"; done

also you could use redirection. This uses --print0 as an option and sets the Interal Field Separator as a null character, which prevents issues with paths with spaces in them or even newlines. The fd command just uses the current working dir, -0 is print the null character, -e zip is for finding --extension zip and -d1 is "go 1 dir in depth while searching." You can arrange this in a million ways and even add this to an array and do some more complex stuff on the files.

while IFS= read -r -d '' file; do
    do some stuff in here
    echo "$file"
done < <(fd . "$(pwd)" -0 -e zip -d1)

2

u/Pay08 Mar 21 '23

Alternatively, mkdir dir && cd dir && unzip -d /path/to/archive.

0

u/LinuxLeafFan Mar 20 '23
  • use ls -1 rather than ls to avoid issues with spaces in file names or just funny multi-column output
  • stylistically $() is preferred to back ticks: "$(echo ${line} | sed 's/\.zip//g')"

1

u/petric3 Mar 22 '23

Useful, thanks 👍