The biggest advantage of this over the functions is that it is portable.
How often do you make a directory (or series of directories) and then change into it to do whatever? 99% of the time that is what I do. This BASH function 'md' will make the directory path then immediately change to the new directory. By using the 'mkdir -p' switch, the intermediate directories are created as well if they do not exist. Show Sample Output
Not a discovery but a useful one nontheless.
In the above example date format is 'yyyymmdd'. For other possible formats see 'man date'.
This command can be also very convenient when aliased to some meaningful name:
alias mkdd='mkdir $(date +%Y%m%d)'
A wrapper around ssh to automatically provide logging and session handling.
This function runs ssh, which runs screen, which runs script.
.
The logs and the screen session are stored on the server.
This means you can leave a session running and re-attach to it later, or from another machine.
.
.
Requirements:
* Log sessions on a remote server
* Transparent - nothing extra to type
* No installation - nothing to copy to the server beforehand
.
Features:
* Function wrapper delegating to ssh
- so nothing to remember
- uses .ssh/config as expected
- passes your command line option to ssh
* Self-contained: no scripts to install on the server
* Uses screen(1), so is:
- detachable
- re-attachable
- shareable
* Records session using script(1)
* Configurable log file location, which may contain variables or whitespace
L="$HOME" # local variable
L="\$HOME" # server variable
L="some space"
.
Limitations:
* Log dir/file may not contain '~' (which would require eval on the server)
.
.
The sessions are named by the local user connecting to the server.
Therefore if you detach and re-run the same command you will reconnect to your original session.
If you want to connect/share another's session simply run:
USER=bob ssh root@server
.
The command above is stripped down to an absolute minimum.
A fully expanded and annotated version is available as a Gist (git pastebin):
https://gist.github.com/flatcap/3c42326abeb1197ee714
.
If you want to add timing info to script, change the command to:
ssh(){ L="\$HOME/logs/$(date +%F_%H:%M)-$USER";/usr/bin/ssh -t "$@" "mkdir -p \"${L%/*}\";screen -xRRS $USER script --timing=\"$L-timing\" -f \"$L\"";}
Show Sample Output
This creates a bash function `take` that you can call with the name of the directory as the first parameter. Add the function to ~/.bashrc to have it available anytime. Show Sample Output
Removes all directories on given path, working from right to left, and stops when reaching a non-empty directory
Counterpart of
mkdir -p new/directory/path
Shortcut (must be issues as next command immediately after mkdir):
^mk^rm
( see http://www.commandlinefu.com/commands/view/19/runs-previous-command-but-replacing )
Make it a reusable function and add the -p flag to mkdir to create directories recursively usage: mydir some/dir/to/create
creates 100 directories f(1-100) with a file in each matched to the directory (/f1/myfile1, .. /f98/myfile98,/f99/myfile99/,/f100/myfile100,etc ) Show Sample Output
Creates a directory and then cds into it directly Show Sample Output
I realise that this is just a reiteration of another entry (regardless of whether I came up with all this all by myself), but I would like present my additional alias' in context as a method of managing your directories. Rather convenient.
Only requirement is bash shell. No functions needed. Show Sample Output
Great for backup / restore scripts. May want to remove the %M/%S to group backups by hour. If using a script, set a variable earlier with the date command, then reference that variable. Otherwise, time will keep on rolling ;-) declare -rx script_start_time="$(date '+./%Y/%m/%d/%H/%M/%S')" mkdir -p "$script_start_time" Show Sample Output
The shell {} operator is great for this. Here's an example with three directories enclosed in {}:
Take a folder full of files and split it into smaller folders containing a maximum number of files. In this case, 100 files per directory. find creates the list of files xargs breaks up the list into groups of 100 for each group, create a directory and copy in the files Note: This command won't work if there is whitespace in the filenames (but then again, neither do the alternative commands :-)
combines mkdir and cd added quotes around $_, thanx to flatcap! Show Sample Output
Create a directory called "dirname" and navigate into it.
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: