Post your commandline one-liners

Hello my fellow Linux people -

A programmer mentioned a useful utility he once had that would display only the items in your current directory that you've modified that day. I jacked his idea and came up with this one-liner (and alias) and figured I'd share:

For .bashrc

alias lt="ls -l | grep \"\$(date | cut -d' ' -f2,3)\""

Command-line:

ls -l | grep "$(date | cut -d' ' -f2,3)"

If you have a useful one-liner or alias please feel free to contribute as well.

2 Likes

My favorite is

dd if=/dev/zero of=/dev/sdon'tBeStupid
1 Like

Probably terrible idea but i like to use
rm -rf *
or when am too lazy and just need something in my home map...
ls */*/*/ | grep "what i need"

1 Like

Might be a bit of a hack and could probably be prettier. But here is a wget command to download all pdfs within one layer from a URL and put them in a directory.

wget -r --level=1 -H -nd -N -np -A .pdf -e robots=off --wait 0.25 [URL]

-r --level=1 = recurse through directories, 1 link deep
-H = host spanning
-nd = don't create directories
-N = new copy
-np = stay in specified directory, no parents
-A = filetype filter
-e robots=off = ignore all rules
--wait 0.25 = rate limiting to reduce server load
2 Likes

You're a horrible person ignoring the robots.txt :p ;)

But you reminded me of:
Lynx -listonly -dump $URL > $FILE
To get all 'Links' of a website and place them in a file. Also Lynx is a good browser for cheap bash scripts.

> file

You're right, now that I look at it again that looks pretty bad. I can't remember why I put that in. I think that I was basically looking around on different forums for examples until I found things that did what I wanted and patched them together.

I won't use that bit any more. I remember putting the wait command in to not be a complete asshole.

it was a joke :p but for admin's love its just best to respect the robots.txt unless you really want the stuff then go ahead and the wait command is also for you're own best interest. You don't want an warning that you need to resolve an anti-robot puzzle.

you could also use

find . -mtime -1

I was hoping for unix pick up lines.

2 Likes

mines simple....

alias ls="ls --color -al";

im sorry D:

1 Like

sudo lssrc -a |more

2 Likes

A very barebones backup command:

rsync -a --delete /home/user/sync_src/ /home/user/sync_dst/ --log-file="/home/user/logs/rsync_`date '+%Y-%m-%d_%H-%M-%S'`.txt" && /usr/bin/7z a -t7z -mx5 /home/user/sync_archives/"archive-sync_dest-`date '+%Y-%m-%d %H;%M;%S'`.7z" /home/user/sync_dst/ && ls -dt /home/user/sync_archives/* | tail -n +7 | xargs -d '\n' rm -- > /dev/null 2>&1
  • makes a copy of /home/user/sync_src/ folder to /home/user/sync_dst/ with rsync and produces a log on /home/user/logs/ of the operation with date and time on the filename
  • Makes an archive of home/user/sync_dst/ folder with 7z for good compression (or use tar a -pcz when persevering file permission is required) with date and time on filename archive
  • Keep the 6 most recent archives on the /home/user/sync_archives/ folder and deletes the old ones
3 Likes

I use this quite often.

boiler plate code is
[command] | while read line; do [command] $line; done

examples
- find | while do read line; do string $line; done | grep -i "flag" > all_flags.txt
- ls | grep "comp" | grep -v "tar" | while read line; do diff -r comp_1 $line; done

Gateway to oneliners : http://www.commandlinefu.com/commands/browse
Gateway to bug check oneliners and full scripts : http://www.shellcheck.net/

1 Like

the one i use the most often would be

cat ** | grep 'Information'

will cat the entire dir. very useful when looking for a bunch of information in a directory of log files

I think this one is actually from Ubuntu... I have it in an alias (alert) and I use it whenever I run long commands so I get notified when its finished. Then I don't have to keep my eye on it for the most part:

notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"

I use it for lines like this:

sudo dd if="$1" of="$2" status=progress && eject "$2" && alert "dd finished"

Sometimes I do some searching through logs for IP's...

# get all IP's from a particular column (4)
cat ergerg.log | awk '{print $4}' | grep -oE --color=never "([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}" | sort | uniq
# get highest recurring IP
cat ergerg.log | grep -oE --color=never "([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}" | sort | uniq -c | sort -nr | head -10
1 Like

move /y *.jpg D:\pr0n

1 Like

@LordXenu

for fname in *.jpg; do mv "$fname" $(echo "$fname" | sha1sum | cut -f1 -d' ').jpg; done

1 Like

@The_Cable You can do ls -R | grep "Wendell's Beard" and it will accomplish the same thing.

Not exactly one-liners, but my ~/.ssh/config file is 235 lines long because I'm an openstack admin and I juggle maybe 20 RSA keys on a daily basis.

I use udisksctl mount -d /dev/$drive too often. I've aliased alias udiskmnt=udisksctl mount -d so because typing udisksctl is not natural on qwerty.

1 Like