Difference between revisions of "Miscellaneous commands"
(→Selecting random lines from a file) |
|||
Line 92: | Line 92: | ||
done | done | ||
echo | echo | ||
+ | |||
+ | == Linux I/O dedirection == | ||
+ | * The following command saves <tt>stdout</tt> and <tt>stderr</tt> to the files "<tt>out.txt</tt>" and "<tt>err.txt</tt>", respectively: | ||
+ | |||
+ | ./cmd 1>out.txt 2>err.txt | ||
+ | |||
+ | * The following command ''appends'' <tt>stdout</tt> and <tt>stderr</tt> to the files "<tt>out.txt</tt>" and "<tt>err.txt</tt>", respectively: | ||
+ | |||
+ | ./cmd 1>>out.txt 2>>err.txt | ||
+ | |||
+ | * The following command functions similar to the above two commands, but also ''copies'' <tt>stdout</tt> and <tt>stderr</tt> to the files "<tt>stdout.txt</tt>" and "<tt>stderr.txt</tt>", 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 | ||
+ | ''Note: The above should be entered as one command (ie, the line that ends with a backslash is only continued on the next line because of the formatting constraints of this page).'' | ||
+ | |||
+ | Also note that Linux uses the following redirection codes/handles (see: [[Redirection (Linux)|redirection]]): | ||
+ | * 0 = <tt>stdin</tt> | ||
+ | * 1 = <tt>stdout</tt> | ||
+ | * 2 = <tt>stderr</tt> | ||
== See also == | == See also == | ||
Line 100: | Line 120: | ||
* [[Xargs (command)|xargs]] | * [[Xargs (command)|xargs]] | ||
* [[Bash]] | * [[Bash]] | ||
+ | * [[Tee (command)|tee]] | ||
[[Category:Linux Command Line Tools]] | [[Category:Linux Command Line Tools]] |
Revision as of 05:54, 18 August 2006
This article will present various miscellaneous commands tested in Linux (running SuSE 10.1). Most of these will be simple command line tools. Eventually, many of these will have their own article. For now, they are presented as is with absolutely no guarantee and zero responsibility on my part if they cause loss of information or files. Use at your own risk.
Contents
Bulk image resize
If you are like me and have a high resolution digital camera, it is often necessary to resize the images before emailing them to friends and family. It is, of course, possible to manually resize them using Adobe Photoshop, The Gimp, or any other image editing programme. However, it is possible to automate this task using simple command line tools.
For an example, say you want to resize all of the jpeg images in your current directory to 800x600 and place them in a sub-directory called, "resized". Then you would execute the following commands:
find . -maxdepth 1 -name '*.jpg' -type f -exec convert -resize 800x600 {} resized/{} \;
It is also possible to have the above commands run recursively through a directory and its sub-directories like so:
find . -follow -name '*.jpg' -type f -exec convert -resize 800x600 {} ../resized/{} \;
Note that the programme convert is part of the ImageMagick suite and you will need to have it installed to use the above commands (it is, by default, in SuSE Linux).
Tracking down large files
Sometimes it is necessary to find files over a certain size and it can be somewhat tedious ls-ing through your many directories. The following command will list only those files over a certain size and only within the specified directory (and sub-directories):
find some_directory/ -size +2000k -ls
which will only list files over 2000 kb (2 MB).
Finding files containing a string in a directory hierarchy
In this example, all .php files will be searched for the string "MySQL" (case-insensitive with -i) and the line numbers will also be returned (using -n):
find . -name '*.php' -type f | xargs grep -n -i 'MySQL'
Selecting random lines from a file
This example could be used for printing random quotes from a file (note: the following should be issued as a single command):
FILE="/some/file_name"; nlines=$(wc -l < "$FILE"); IFS=$'\n'; array=($(<"$FILE")); echo "${array[$((RANDOM%nlines))]}"
Here, nlines holds the total number of lines in the file. The file is read into an array (note the use of IFS — this splits the lines based on '\n'). Then, once the array has been populated, print a random line from it.
Printing a block of text from a file
Say you have a file, foo, and it contains the following lines (note the captial letters and the full stop in line six):
one blah blah Two blah blah three blah blah 3, 4 Four blah blah five blah blah 5, 6 six blah blah.
If you only want to print out lines 3, 4, and 5, execute the following command:
awk "/three/,/five/" < foo
If you only want to print out lines starting with a capital "F", execute the following command:
awk "/^F/" < foo
If you only want to print out lines ending in a full stop, execute the following command:
awk "/\.$/" < foo
Finally, if you only want to print out lines containing the numbers "5" and "6", execute the following command:
awk "/[5-6]/" < foo
Multiple unzip
The following command will unzip all zip files in the current directory.
for i in $(ls *.zip) do unzip $i done
Or, as a single command:
for i in $(ls *.zip); do unzip $i; done
Bash shell colour codes
This following code echoes a bunch of colour codes to the terminal to demonstrate what's available. Each line is the colour code of one forground colour, out of 17 (default + 16 escapes), followed by a test use of that colour on all nine background colours (default + 8 escapes).
#!/bin/bash T='gYw' # The test text echo -e "\n 40m 41m 42m 43m\ 44m 45m 46m 47m"; for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' \ '1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' \ ' 36m' '1;36m' ' 37m' '1;37m'; do FG=${FGs// /} echo -en " $FGs \033[$FG $T " for BG in 40m 41m 42m 43m 44m 45m 46m 47m; do echo -en "$EINS \033[$FG\033[$BG $T \033[0m"; done echo; done echo
Linux I/O dedirection
- The following command saves stdout and stderr to the files "out.txt" and "err.txt", respectively:
./cmd 1>out.txt 2>err.txt
- The following command appends stdout and stderr to the files "out.txt" and "err.txt", respectively:
./cmd 1>>out.txt 2>>err.txt
- The following command functions similar to the above two commands, but also 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
Note: The above should be entered as one command (ie, the line that ends with a backslash is only continued on the next line because of the formatting constraints of this page).
Also note that Linux uses the following redirection codes/handles (see: redirection):
- 0 = stdin
- 1 = stdout
- 2 = stderr