Manuals and HowTo's : Linux Filesystem
Ludicrous Home > Manuals and HowTo's > Linux Filesystem

Some tricks that make using the Linux command line a really useful tool.



Commands for the Linux shell

The following commands have been tested only with the bash shell.

How to find and remove empty folders in a directory tree

find-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 permission

chmod g+w

This would only set the groups write permission.

How to change ownership of file and group in one step

chown username.groupname

What is the longest path on your system

find /| awk '{print length($0) " " $0}' | sort -n | tail -n 1

On my system this yields in:

240 /cygdrive/c/ProgramFiles/Development/Framework/eclipse/plugins/org.eclipse.platform.source_3.2.1.r321_v20060921-b_XVA-INSQSyMtx
/src/org.eclipse.core.runtime.compatibility.registry_3.2.1.R32x_v20060907/
runtime_registry_compatibilitysrc.zip

How to redirect outputs from command line tools

These redirections are tested for the bash shell, but maybe work also with other shells.

Symbol Explanation Example
> stdout to file
ls -l > list.txt
2> stderr to file
grep da * 2> grep-errors.txt
1>&2 stdout to stderr
grep da * 1>&2
2>&1 stderr to stdout
grep * 2>&1
&> stdout and stderr to file
rm -f $(find / -name core) &> /dev/null

Redirect only stderr to a pipe

Pipes 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

n<&- Close input file descriptor n.
0<&, <& Close stdin.
n>&- Close output file descriptor n.
1>&, >& Close stdout.

Another cool command

The 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 Substitution

Bash 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`"
If the output of the substituted command contains 0x00-bytes these will be silently purged by the shell and will never reach the outermost command!

Basic arithmetic evaluation

Bash even provides us with very basic arithmetic support:

echo "$((155 * 345))"

Feed continuously strings to stdout

In 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.

Troubleshooting

Some shared objects can't be found

For 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 systems

How to read a ext3fs linux partition from Windows

Have a look at this filesystem driver.



Document generated by Atlassian Confluence, last changed on may 04, 2007 by Sven Rieke