| Ludicrous Home > Manuals and HowTo's > Linux Filesystem |
|
Linux Filesystem
Some tricks that make using the Linux command line a really useful tool.
Commands for the Linux shellThe following commands have been tested only with the bash shell. How to find and remove empty folders in a directory treefind-type d -empty -delete If we also want to remove empty files we just don't use the -type d option. How to change only one specific file permissionchmod g+w This would only set the groups write permission. How to change ownership of file and group in one stepchown username.groupname What is the longest path on your systemfind /| awk '{print length($0) " " $0}' | sort -n | tail -n 1
On my system this yields in:
How to redirect outputs from command line toolsThese redirections are tested for the bash shell, but maybe work also with other shells.
Redirect only stderr to a pipePipes normally only work with stdout, so here we show how to redirect the stderr to a pipe: exec 3>&1 # Save current "value" of stdout. ls -l 2>&1 >&3 3>&- | grep bad 3>&- # Close fd 3 for 'grep' (but not 'ls'). # ^^^^ ^^^^ exec 3>&- # Now close it for the remainder of the script. Closing File Descriptors
Another cool commandThe following command copies stdout and stderr to the files "stdout.txt" and "stderr.txt", respectively. (((./cmd | tee stdout.txt) 3>&1 1>&2 2>&3 | tee stderr.txt) 3>&1 1>&2 2>&3) 1>out.txt 2>err.txt
Command SubstitutionBash uses named pipes in a really neat way. Recall that when you enclose a command in parenthesis, the command is actually run in a ``subshell''; that is, the shell clones itself and the clone interprets the command(s) within the parenthesis. Since the outer shell is running only a single ``command'', the output of a complete set of commands can be redirected as a unit. For example, the command: (ls -l; ls -l) >ls.out writes two copies of the current directory listing to the file ls.out. Command substitution occurs when you put a < or > in front of the left parenthesis. For instance, typing the command: cat <(ls -l) The command: ls | tee >(grep foo | wc >foo.count) \ >(grep bar | wc >bar.count) \ | grep baz | wc >baz.count counts the number of occurrences of foo, bar and baz in the output of ls and writes this information to three separate files. Command substitutions can even be nested: cat <(cat <(cat <(ls -l)))) works as a very roundabout way to list the current directory. Another form of command substitution is using "`": echo "`date`"
Basic arithmetic evaluationBash even provides us with very basic arithmetic support: echo "$((155 * 345))" Feed continuously strings to stdoutIn its default behavior the yes command feeds a continuous string of the character y followed by a line feed to stdout. A control-c terminates the run. A different output string may be specified: yes "sample" yes $BASH_VERSION One might well ask the purpose of this. From the command line or in a script, the output of yes can be redirected or piped into a program expecting user input. TroubleshootingSome shared objects can't be foundFor example, you want to test a program you copied from another system with its shared object libraries, but you don't want to install these in to your paths, you can change the LD_LIBRARY_PATH variable point to these: export LD_LIBRARY_PATH=/path_where_the_so_are:$LD_LIBRARY_PATH Interacting with Linux systemsHow to read a ext3fs linux partition from WindowsHave a look at this filesystem driver.
|
| Document generated by Atlassian Confluence, last changed on may 04, 2007 by Sven Rieke |