commandlinefu.com is the place to record those command-line gems that you return to again and again.
Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/
You can sign-in using OpenID credentials, or register a traditional username and password.
First-time OpenID users will be automatically assigned a username which can be changed after signing in.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for:
While `echo rm * | batch` might seem to work, it might still raise the load of the system since `rm` will be _started_ when the load is low, but run for a long time. My proposed command executes a new `rm` execution once every minute when the load is small.
Obviously, load could also be lower using `ionice`, but I still think this is a useful example for sequential batch jobs.
# put into .bashrc
function trash() {
if [ -z "$*" ] ; then
echo "Usage: trash filename"
else
local TRASH="${HOME}/.local/share/Trash"
if [ ! -d "$TRASH/files" ]; then mkdir -p "$TRASH/files"; fi
if [ ! -d "$TRASH/info" ]; then mkdir -p "$TRASH/info"; fi
local IFS_BKP=$IFS
IFS='
'
for FILE in $@ ; do
local BASE=$( basename "$FILE" )
local TRASH_NAME="$BASE"
local COUNTER=1
while [ -e "$TRASH/files/$TRASH_NAME" ]; do
COUNTER=`expr $COUNTER + 1`
TRASH_NAME="$BASE.$COUNTER"
done
local FULL_PATH=$( readlink -f "$FILE" )
local DATE=$( date +%Y-%m-%dT%H:%M:%S )
mv "$FULL_PATH" "$TRASH/files/$TRASH_NAME"
if [ $? -eq 0 ]; then
echo "[Trash Info]
Path=$FULL_PATH
DeletionDate=$DATE" > "$TRASH/info/$TRASH_NAME.trashinfo"
fi
done
IFS=$IFS_BKP
fi
}
Uses zsh globbing syntax to safely remove all the files known to be generated by LaTeX, but only if there is actually a .tex source file with the same basename present. So we don't accidentally delete a .nav .log or .out file that has nothing to do with LaTeX, e/'[[ -f ${REPLY:r}.tex ]]'/ actually checks for the existance of a .tex file of the same name, beforehand.
A different way to do this, would be to glob all *.tex files and generate a globbing pattern from them:
TEXTEMPFILES=(*.tex(.N:s/%tex/'(log|toc|aux|nav|snm|out|tex.backup|bbl|blg|bib.backup|vrb|lof|lot|hd|idx)(.N)'/)) ;
rm -v ${~TEXTEMPFILES}
or, you could use purge() from grml-etc-core ( http://github.com/grml/grml-etc-core/blob/master/usr_share_grml/zsh/functions/purge )
recursively delete empty directories and directories which only contain empty directories using zsh globbing syntax. ** is for recursive globbing. *(/^F) matches all entries that are directories which are not full.
If you only want to delete empty directories and not those directories which contained only empty directories and will be empty afterwards, just leave out the options to rmdir:
rmdir **/*(/^F)
recursively deletes all broken symlinks using zsh globbing syntax.
Replace rm, a neat shortcut, with a less permanent method of removal.
Note may require you to install the trash client; "sudo apt-get -y install trash-cli"
rm /SOME/PATH/*, when you hit "argument list too long".
This command won't delete resource forks from an HFS file system, only from file systems that don't natively support resource forks.
To prevent accidental deleting of files you can disable rm with this alias. Then use the trash command from trash-cli instead.
You could do the following, however, brace expansion with {} is not defined in POSIX, and therefore not guaranteed to work in all shells. But, if it does, it's more convenient (although it's certainly not less typing):
cp -r {*,.??*} /dest
Sometimes there are times when I need to cp(1), mv(1) or rm(1) files recursively, but don't want to traverse the previous directory by following ../../../../ etc out of the current directory. This command prevents that. The secret sauce is ".??*". The file globbing ensures that it must start with a dot, and be followed by at least two characters. So, three characters must exist in the filename, which eliminates "." and "..".
Sometimes you unzip a file that has no root folder and it spews files all over the place. This will clean up all of those files by deleting them.
rm -rf .* matches ".." and thus one goes up a level and wipes out more than intended.
In bash, .??* safely accomplishes what one intends - remove those .files
The ? matches most characters except "/", thus .?? does not match ../ and so one is safe.
Remove all arquives except the list.
Can't have space between the commas.
Bash method to remove all files but "abc".
It would be 'rm *~abc' in Zsh.
-depth argument will cause find to do a "depth first" tree search, this will eliminate the "No such file or directory" error messages
expands through shell and not find
but may hits the limit of max argument size for rm
(thus: for f in **/*.htm;do rm $f;done
but then I prefer the find command ;)