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:
xargs is a more elegant approach to executing a command on find results then -exec as -exec is meant as a filtering flag.
example of using zsh extended globbing
`pwd` returns the current path
`grep -o` prints each slash on new line
perl generates the paths sequence: './.', './../.', ...
`readlink` canonicalizes paths (it makes the things more transparent)
`xargs -tn1` applies chmod for each of them. Each command applied is getting printed to STDERR.
(Please see sample output for usage)
Use any script name (the read command gets it) and it will be encrypted with the extension .crypt, i.e.:
myscript --> myscript.crypt
You can execute myscript.crypt only if you know the password. If you die, your script dies with you.
If you modify the startup line, be careful with the offset calculation of the crypted block (the XX string).
Not difficult to make script editable (an offset-dd piped to a gpg -d piped to a vim - piped to a gpg -c directed to script.new ), but not enough space to do it on a one liner.
Sorry for the chmod on parentheses, I dont like "-" at the end.
Thanks flatcap for the subshell abbreviation to /dev/null
(Please see sample output for usage)
script.bash is your script, which will be crypted to script.secure
script.bash --> script.secure
You can execute script.secure only if you know the password. If you die, your script dies with you.
If you modify the startup line, be careful with the offset calculation of the crypted block (the XX string).
Not difficult to make script editable (an offset-dd piped to a gpg -d piped to a vim - piped to a gpg -c directed to script.new ), but not enough space to do it on a one liner.
This command is useful to recursively make executable all "*.sh" files in a folder.
This command is useful to apply chmod recursively in a determined kind of file.
Linux users wanting to extract text from PDF files in the current directory and its sub-directories can use this command. It requires "bash", "ps2ascii" and "par", and the PARINIT environment variable sanely set (see man par). WARNING: the file "junk.sh" will be created, run, and destroyed in the current directory, so you _must_ have sufficient rights. Edit the command if you need to avoid using the file name "junk.sh"
Allows to change 'shell' compatible files execution bit even if their name is not *.sh
An example of this command that includes the -name arg.
Using $_ in the chmod command saved a good bit of typing ? obviously the $_ will contain the path to the file we?re talking about, as it was the last argument to the previous command.
source: http://www.preshweb.co.uk/2007/07/bashs-_-variable-last-argument/
Using `-exec cmd {} +` causes find to build the command using all matching filenames before execution, rather than once per file.
Using xargs is usually much quicker as it does not have to execute chmod for every file
This command finds all files in a folder recursively and sets owner and group to read and write. Leaves all dirs intact. This command does takes care of file names with spaces as well.
Mac install ssh-copy-id
From there on out, you would upload keys to a server like this:
(make sure to double quote the full path to your key)
ssh-copy-id -i "/PATH/TO/YOUR/PRIVATE/KEY" username@server
or, if your SSH server uses a different port (often, they will require that the port be '2222' or some other nonsense:
(note the double quotes on *both* the "/path/to/key" and "user@server -pXXXX"):
ssh-copy-id -i "/PATH/TO/YOUR/PRIVATE/KEY" "username@server -pXXXX"
...where XXXX is the ssh port on that server
this requires the use of a throwaway file.
it outputs a shell function.
assuming the throwaway file is f.tmp
usage: >f.tmp;lso f.tmp > f.tmp; . f.tmp;rm f.tmp;lso -l ...
notes:
credit epons.org for the idea. however his version did not account for the sticky bit and other special cases.
many of the 4096 permutations of file permissions make no practical sense. but chmod will still create them.
one can achieve the same sort of octal output with stat(1), if that utility is available.
here's another version to account for systems with seq(1) instead of jot(1):
lso(){
case $# in
1)
{ case $(uname) in
FreeBSD)
jot -w '%04d' 7778 0000 7777 ;;
*)
seq -w 0000 7777 ;;
esac; } \
|sed '
/[89]/d
s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \
|sh \
|{
echo "lso(){";
echo "ls \$@ \\";
echo " |sed '";
sed '
s, ,@,2;
s,@.*,,;
s,\(.* \)\(.*\),s/\2/\1/,;
s, ,,';
echo \';
echo };
};
;;
*)
echo "usage: lso tmp-file";
;;
esac;
}
this won't print out types[1]. but its purpose is not to examine types. its focus is on mode and its purpose is to make mode easier to read (assuming one finds octal easier to read).
1. one could of course argue "everything is a file", but not always a "regular" one. e.g., a "directory" is really just a file comprising a list.