Obscure linux commands

apropos- display a list of all topics in the man pages by command names based on keyword search usage: apropos keyword

apropos pstree
apropos editor
————-

bg/fg – background or foreground a job (command) – adding an ampersand ( & ) to the command runs the job in the background

bg [options] [job]
1) start application
2) ctrl +z – stop the current job
3) bg – moves the command to background and continue running
4) jobs – display the jobs in the shell

fg – Continues a stopped job by running it in the foreground
fg ‘specify job #’ – %job Specifies the job that you want to run in the foreground.
fg 1 – will run job 1 process in the foreground

————–
egrep – search files for lines that match regular expressions. Runs faster than grep and fgrep
egrep [options] pattern [file]

OPTIONS:

-b Print the byte offset of input file before each line of output.
-c Print’s the count of line matched.
-e pattern list Searches the pattern list.
-f file Take the list of full regular expressions from file.
-h Print matched lines but not filenames.
-i Ignore changes in case; consider upper- and lower-case letters equivalent.
-n Print line and line number.
-q Prints in quite mode, prints nothing.
-r Recursively read all files in directories and in subdirectories found.
-v Prints all the lines that do not match.
-V Print Version.
-w Match on whole word only.
strings Specify a pattern to be used during the search for input.
file A path name of a file to be searched for the patterns. If no file operands are specified, the standard input will be used.

Regular expressions can also be used.

egrep "list|quote|print" file.txt – Would search for patterns of list quote and print in the file file.txt.

fgrep – a faster version of grep which does not support regular expressions, fgrep is equal to grep -F

rgrep – a recursive version of grep. rgrep can recursively descend through directories as it greps for the specified pattern. rgrep is similar to grep -r.

more info here:
http://www.linuxconfig.org/Grep_egrep_fgrep_rgrep
http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/

————–

fc – views, edits, and executes commands from the history list. This is a shell builtin.

fc [-e ename] [-nlr] [first] [last] – a range of commands from first to last is selected from the history list

fc -s [pat=rep] [cmd] – command is re-executed after each instance of ‘pat’ is replaced by ‘rep’

Options:
-e ENAME select which editor to use. Default is FCEDIT, then EDITOR, then vi
-l list lines instead of editing
-n omit line numbers when listing
-r reverse the order of the lines (newest listed first

————–

od/hexdump/xxd – basically does hexdumps with different features – make a hexdump or do the reverse
xxd -h[elp]
xxd [options] [infile [outfile]]
xxd -r[evert] [options] [infile [outfile]]
xxd -r no parse errors

—————

ldd – List dependencies or the shared libraries used by a program,
must use full path or be in dir if command is not in path

ldd `which freshclam`
ldd -v /usr/bin/nano

~$ cd /usr/bin
/usr/bin$ ldd -v nano

——————-

logsave – this command saves the output of a command to a log file:

$ logsave /var/log/partsize df –h
Here, the logsave command saves the output of the df command to the file /var/log/partsize file – it will also add a timestamp and output the results of the command to standard out.

Doesn’t sound overly interesting, does it? Well the magic of logsave is that if the file, in our case /var/log/partsize, does not exist then logsave will queue the data in memory and wait for the file to appear. This is perfect for tracking the output of commands run doing the boot process when the partition you are writing to is not yet mounted.

—————-

lsof – list files currently in use

lsof -p -processid - see all files in use by a particular process
lsof -i -Show all connections
lsof -iTCP Show only TCP connections
lsof -i :22 shows all networking related to a given port
lsof -i@192.168.1.8 show connections to a specific host, use @host
lsof -i@192.168.1.8:22 Show connections based on the host and the port using @host:port
lsof -i| grep LISTEN shows what ports your system is waiting for connections on
lsof -i| grep ESTABLISHED shows current active connections
lsof -u david Show what a given user has open
lsof -c syslog-ng what files and network connections a command is using
lsof /var/log/messages what's interacting with that file
lsof -p 10291 what a given process ID has open
lsof -i4 -nP show if any process is holding open tcp 80

lsof -a -u david -i @10.1.13.11 everything running as david connected to 10.1.13.11
kill -9 `lsof -t -u david kill everything a user has open
lsof +L1 shows all open files that have a link count less than 1

—————-

mkfifo – used to create a FIFO (first in, first out) or (named pipe) to permit communications between 2 unrelated processes. When created, this will be a file system persistent object (regular flat file).
mkfifo pipe create a named pipe with the name pipe

—————-

nohup – runs a command that keeps running after logout. The command is in principle immune to hangups, and must have output to a non tty.

nohup command-name &
nohup myscript.sh &

ctrl +D or exit – script continues to run

—————-

pstree – shows running processes as a tree.
pstree [options] [pid or username]
-p shows a PID in parenthesis after each process name
-n sorts output order by PIDs instead of default alphabetic order
-h highlights the current process and all of its ancestors
-l prevents the truncation of long lines
-u tells pstree to show the owner of a process
-a shows the command line arguments for each process that was initiated by a user

pstree joe
pstree root

—————-

script – Records everything printed on your screen
script [-a] filename
-a append the session record to filename, rather than overwrite it.
filename – the name of the file that the results will be stored in.
script ends when the forked shell exits (user types exit) or when CTRL-D is typed

—————–

strace – traces the system calls of a program while the program is running
strace -o strace_ls_output.txt ls (logs the info to a file)
-f option causes strace to trace child process created by the fork(2) system call
-ff splits the traces into separate files according to process
-p option allows one to attach to an already existing process by its pid number
-s with a specified (integer) length adjusts the maximum length of strings to print.

strace will not provide stack traces, gdb (backtrace) is the program to use

—————–

strings – returns each string of printable characters in a binary file

strings [options] file_name(s) displays all strings that are at least four characters in length in the file

strings -n 2 file1 file2 return strings which are at least the number of that integer in length

strings -f tells strings to begin each line of its output with the name of the file from which it is returning a particular string, followed by a colon and then by several spaces

—————–

tee – copy standard input to standard output and one or more files

ls *.txt | wc -l | tee /dev/tty count.txt
the ls command would list all .txt files in the current directory, take a word count (wc) by the amount of lines (-l) and the output displayed to the /dev/tty (terminal) will be sent to the count.txt.
—————–

xload – displays a graphic of the system load if X is enabled

—————–

Another useful control operator is the ‘&’ (ampersand)
Ending a command with ‘&’ runs the command with a new PID (subshell), releasing the command line back to you. Useful for running a background process.

—————–

[Tab][Tab]
root@host# Display all 3119 possibilities? (y or n)

– prints a list of all available commands. This is just an example of autocomplete with no restriction on the first letter.

—————–

x[Tab][Tab] – prints a list of all available completions for a command, where the beginning is “x”

—————–

[Ctrl]c – kill the current process
[Ctrl]d – logout from the current terminal
[Ctrl]s – stop transfer to current terminal
[Ctrl]q – resume transfer to current terminal. This should be tried if the terminal stops responding.
[Ctrl]z – send current process to the background

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....