This command is useful if you accidentally untar or unzip an archive in a directory and you want to automatically remove the files. Just untar the files again in a subdirectory and then run the above command e.g.
for file in ~/Desktop/temp/*; do rm ~/Desktop/`basename $file`; done
it will remove everything except the file names matching you can use also use wildcards
Remove everything except that file with shell tricks inside a subshell to avoid changes in the environment.
help shopt
Sometimes Firefox crashes or is bad finished and the message the process is still running appear while it's not. This also works when you sharing account from a NIS server and try to open the browser on multiple computers.
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 "..".
An alternative which uses the advanced zsh globbing (pattern matching)
-depth argument will cause find to do a "depth first" tree search, this will eliminate the "No such file or directory" error messages
rsync'ing an empty directory over a directory to be deleted recursively is much faster than using rm -rf, for various reasons. Relevant only for directories with really a lot of files.
Deletes empty directories and prints an error if directory is not empty. Show Sample Output
This will search all directories and ignore the CVS ones. Then it will search all files in the resulting directories and act on them.
recursively deletes all broken symlinks using zsh globbing syntax.
Can be used for other commands as well, replace rm with ls. It is easy to make this shorter but if the filenames involved have spaces, you will need to do use find's "-print0" option in conjunction with xargs's "-0" option. Otherwise the shell that xargs uses to execute the "rm" command line will treat the space as a token separator, thereby treating the name as two (or more) names. Show Sample Output
Remove everything in current directory except files starting with "ca".
This is for zsh with extended globbing.
It does not work without the verbose mode (-v is important)
Please be careful while executing the following command as you don?t want to delete the files by mistake. The best practice is to execute the same command with ls ?l to make sure you know which files will get deleted when you execute the command with rm.
Bash method to remove all files but "abc". It would be 'rm *~abc' in Zsh.
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. Show Sample Output
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. Show Sample Output
commandlinefu.com is the place to record those command-line gems that you return to again and again. 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.
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: