The following (and quite a bit of what will come in future on Linux related topics) is summarised from various linux for noobs websites, magazines and blogs. None of it is taken as written, unless quoted, apart from the commands. The main reason for plonking it all on this site is so that I have a reference for when I need certain commands and knowledge on how to get stuff done. It would be great to be corrected if any of this is out of date or if there is a better way to do it.
Redirection
- command > file : send command output to a file
- command 2> file : send the error output from a command to a file
- command > file 2>&1 : send both command and error output to a file
- command < file : read inputs to a command from a file
- command < file.in > file.out : read file contents into a command and save output to a file
- command >> file : append the results of a command to the end of a file
- command 2>> file : append error output to the end of a file
- command | command2 : pipe command output into command2
File manipulation examples
- sort < /etc/passwd : this is an example of redirecting the command input from a file. It outputs a sorted list of lines in the file /etc/passwd
- grep typedef /usr/include/* > output.file : this example searches for typedef in all the files in /usr/include and then saves the search results into a file which can be used as input or opened for analysis
- find / -name COPYING -print 2> output.txt : this command example searches through the file system (starting at root, /) to find files named COPYING and outputs the error messages into output.txt. The errors should be access denied type messages in this case. Use nano output.txt (or the editor of your choice) to view the contents of that file. To ignore the error messages send them to /dev/null
History commands
- history 5 : shows the last 5 commands
- history > hist.txt : outputs the full command history into hist.txt
- !! : repeat last command
- !command : repeat last instance of a specific command
- !23 : repeat command 23 (as found in hist.txt)
- !! | command : repeat the last command and pipe it into command
Leave a Reply