Ask the Community
Groups
General Linux Commands - Connect IT Community | Kaseya
<main> <article class="userContent"> <h2 data-id="summary"><strong>SUMMARY</strong></h2> <p>General Linux Commands</p> <h2 data-id="issue"><strong>ISSUE</strong></h2> <p></p> <h2 data-id="general-commands"><span id="General_Commands">General Commands</span></h2> <h3 data-id="ls"><span id="ls">ls</span></h3> <p>lists files, directories, etc. within a directory.</p> <dl><dd>-l long listing showing permissions, size, etc. (required for all the rest of these flags)</dd> <dd>-h human-readable values for size</dd> <dd>-S sort by size, largest to smallest</dd> <dd>-t sort by time modified, newest to oldest</dd> <dd>-r sort by reverse of other flag (i.e. ls -ltr gives files sorted from oldest to newest)</dd> <dd>-A show all files, including hidden files (will not show . and .. as -a does)</dd> <dd>-R recursive listing, shows all files in all directories and subdirectories of the current path</dd> <dd>-i shows inode numbers (inodes are structures that store information about files, directories, etc.)</dd> </dl><h3 data-id="cd"><span id="cd">cd</span></h3> <p>changes directory</p> <dl><dd>cd .. changes to parent directory</dd> <dd>cd <relative path like backups> goes to relative path within directory</dd> <dd>cd <absolute path like /backups> goes to absolute path</dd> <dd>cd without any flags goes to the logged-in user's home directory (also represented by ~)</dd> <dd>cd - goes to previous directory (directory before the last change of directory)</dd> </dl><h3 data-id="mkdir"><span id="mkdir">mkdir</span></h3> <p>create directory</p> <pre class="code codeBlock" spellcheck="false" tabindex="0"> mkdir /tmp/1 would create a directory called 1 in /tmp </pre> <dl><dd>-p creates parent directories as needed</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> mkdir /tmp/1/2/3/4 would create all of the needed parent directories if they did not exist already </pre> <dl><dd>-v verbose</dd></dl><h3 data-id="rmdir"><span id="rmdir">rmdir</span></h3> <p>remove directory</p> <pre class="code codeBlock" spellcheck="false" tabindex="0"> rmdir /tmp/1 would remove that directory (if it is empty). </pre> <dl><dd>-v verbose</dd></dl><h3 data-id="mv"><span id="mv">mv</span></h3> <p>moves and/or renames files/directories</p> <dl><dd>-f forces the move--no prompt before overwriting</dd> <dd>-v verbose; show what is being done</dd> </dl><h3 data-id="cp"><span id="cp">cp</span></h3> <p>copies files, directories, etc.</p> <dl><dd>-p preserve mode, ownership, timestamps when copying</dd> <dd>-R or -r copies directories recursively</dd> <dd>-v verbose</dd> </dl><h3 data-id="pwd"><span id="pwd">pwd</span></h3> <p>print working directory (current directory)</p> <h3 data-id="rm"><span id="rm">rm</span></h3> <p>removes files, can remove directories as well</p> <dl><dd>-f force removal--do not prompt</dd> <dd>-r recursive removal; allows removal of files and directories. BE CAREFUL WITH THIS!! KNOW WHERE YOU ARE!!</dd> <dd>-v verbose</dd> </dl><h3 data-id="man"><span id="man">man</span></h3> <p>shows manual pages for a given program or topic</p> <dl><dd>running man tar will show the manual pages for the archiving program tar</dd></dl><h3 data-id="cat"><span id="cat">cat</span></h3> <p>concatenates multiple files, prints files to screen</p> <dl><dd>-A shows all non-printing characters like tabs, end-of-line characters, etc.</dd> <dd>-n numbers all lines</dd> </dl><h3 data-id="head"><span id="head">head</span></h3> <p>show the first part of a file; by default the first 10 lines are shown</p> <dl><dd>-n <#> will give the first # lines of a file</dd> <dd>-<#> will also give the first # lines of a file</dd> <dd>i.e., head -n 15 a.txt and head -15 a.txt give the first 15 lines of a.txt</dd> </dl><h3 data-id="tail"><span id="tail">tail</span></h3> <p>show the last part of a file; by default the last 10 lines are shown</p> <dl><dd>-n <#> will give the last # lines of a file</dd> <dd>-<#> will also give the last # lines of a file</dd> <dd>-f will follow a log file as new data is added to it, outputting the data to screen</dd> <dd>i.e., tail -n 15 a.txt and tail -15 a.txt give the last 15 lines of a.txt</dd> </dl><h3 data-id="less"><span id="less">less</span></h3> <p>less is a pager program that will let you move back and forth within a file</p> <dl><dd>i.e., less a.txt will open up a.txt and allow you to page up and down within it.</dd> <dd>Note: typing gg goes to the beginning of the file, G goes to the end, q quits</dd> </dl><h3 data-id="date"><span id="date">date</span></h3> <p>shows the current date and time</p> <dl><dd>to convert from a Unix epoch timestamp to a human-readable format, type date -d <a href="https://kaseya.vanillacommunities.com/profile/%26lt" rel="nofollow">@&lt</a>;UNIX epoch timestamp></dd></dl><h3 data-id="uptime"><span id="uptime">uptime</span></h3> <p>shows how long the system has been up, and the system load average over the last 1 minute, 5 minutes, and 15 minutes</p> <h3 data-id="top"><span id="top">top</span></h3> <p>gives you a running listing of the current processes running, sorted by CPU usage</p> <h3 data-id="ps"><span id="ps">ps</span></h3> <p>similar to top, except that the listing is static.</p> <dl><dd>ps aux is the way I most often run ps.</dd> <dd>ps -leaf is another common way to run ps.</dd> <dd>ps aux | grep <processname> will show you information for the given processname.</dd> </dl><h3 data-id="clear"><span id="clear">clear</span></h3> <p>clears the screen</p> <h3 data-id="df"><span id="df">df</span></h3> <p>shows disk free space, disk used space, etc.</p> <dl><dd>-hT will give you human-readable values and show the filesystem type.</dd> <dd>-BG will give you block sizes in gigabytes.</dd> </dl><h3 data-id="du"><span id="du">du</span></h3> <p>will show disk usage for files, directories, etc.</p> <dl><dd>-s gives high-level summary (1 directory deep)</dd> <dd>-h gives human-readable information.</dd> <dd>-BG gives block-size in gigabytes</dd> <dd>-c gives a total at the bottom.</dd> <dd>du -shc * is very helpful.</dd> </dl><h3 data-id="free"><span id="free">free</span></h3> <p>shows free memory</p> <dl><dd>-m shows memory in megabytes</dd></dl><h3 data-id="echo"><span id="echo">echo</span></h3> <p>prints to screen</p> <dl><dd>-e allows for escape characters (like \n for newline)</dd> <dd>to print the contents of a variable called BASH, do</dd> </dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> echo $BASH </pre> <dl><dd>to append words to a file called a.txt</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> echo end of file is here >> a.txt </pre> <h3 data-id="bc"><span id="bc">bc</span></h3> <p>calculator program best used with echo. by default only gives integers. to give numbers after decimal use scale</p> <dl><dd>divide 86763 by 843 with 3 decimal points in the result</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> echo scale=3; 86763/843 | bc 102.921 </pre> <dl><dd>divide 8000 by 3600 and just leave whole number</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> echo 8000/3600 | bc 2 </pre> <dl><dd>to convert hex FF3 to decimal</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> echo ibase=16; FF3 | bc 4083 </pre> <dl><dd>to convert 1027 to hex</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> echo obase=16; 1027 | bc 403 </pre> <h2 data-id="bash-basics"><span id="Bash_Basics">Bash Basics</span></h2> <p>Bash is the default command line shell for Linux. As you will be logged in via Putty to many backup appliances, you will become very familiar with bash.</p> <ul><li>To redirect the output of a command to a file, use the > sign.</li></ul><dl><dd>b.sh > a.txt will create a.txt and put the standard output of the script b.sh into the file a.txt</dd></dl><ul><li>>> is similar to > but instead appends to a given file.</li></ul><dl><dd>b.sh >> a.txt will add the output of b.sh to a.txt</dd></dl><ul><li>| allows you to run additional commands on the output stream of the previous command. You can use as many pipes as desired.</li></ul><dl><dd>ps aux | grep ssh will run a process listing and then look for the lines containing ssh with the grep utility.</dd></dl><ul><li>using backticks <b>`</b> will do command-line substitution--that is, it will run the command within the backticks</li></ul><dl><dd>i.e., one helpful command to run will give you a listing of files recursively in a given directory, and sort by time.</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> ls -lhtr `find . -type f` </pre> <h2 data-id="using-bash-history"><span id="Using_Bash_History">Using Bash History</span></h2> <dl><dd>the history command will show command history</dd> <dd>!! runs previous command</dd> <dd>!-5 runs the fifth to last command</dd> <dd>!ls runs previous command starting with ls--BE CAREFUL WITH THIS</dd> <dd>^abc^def substitute def for abc in the previous line</dd> <dd>!123 runs history item 123</dd> <dd>Ctrl-R allows you to search your history from most recent command to oldest <dl><dd>hit Ctrl-R, type vim, and if you do not see the right vim command, type Ctrl-R again and it will show you the command for vim prior to that</dd></dl></dd> <dd>Ctrl-K kill (erase) rest of line</dd> <dd>Ctrl-U erase part of line before cursor</dd> <dd>Ctrl-X, Ctrl-U to undo your previous command line edit</dd> <dd>Ctrl-A to go to beginning of line</dd> <dd>Ctrl-E to go to end of line</dd> <dd>Alt-F to go forward a word</dd> <dd>Alt-B to go back a word</dd> <dd>Alt-shift-8 (Alt-asterisk) gives all possible tab completion matches</dd> <dd>Ctrl-Alt-E expands ~ (to the path for the home directory) and asterisks (shell globbing) and command line substitution</dd> </dl><h2 data-id="vim-basics"><span id="vim_basics">vim basics</span></h2> <p>The program vim is a fantastic command-line editor. You can run vim <filename> to open the editor. The editor is opened in command mode, and you can change to insert mode through various methods--most often by hitting the letter <b>i</b>. To return to command mode, hit the ESC key. <b>I strongly recommend using the built-in program vimtutor</b> as this will allow you to learn more about vim; just type vimtutor. While in command mode, you can delete, run special commands to look for lines containing certain information, etc. All of the following is for when you are in command mode.</p> <ul><li>:w saves the file</li> <li>:q! quits, discarding changes</li> <li>:q quits</li> <li>:wq saves the file and quits</li> <li>:e! reopens the current file, discarding changes</li> <li>i goes to input mode at current character</li> <li>A goes to input mode at the end of the current line</li> <li>x deletes the current character</li> <li>dd deletes the current line</li> <li>u undoes the last change; this can be run multiple times</li> <li>Ctrl-R is for redo</li> <li>0 goes to the beginning of the line</li> <li>$ goes to the end of the line</li> <li>gg goes to the beginning of the document</li> <li>G goes to the end of the document</li> <li>/searchpattern searches forward in the document for the searchpattern</li> <li>?searchpattern searches backward in the document for the searchpattern</li> <li>:n goes to the next file if multiple files have been opened.</li> <li>:N or :prev goes to the previous file in multiple files</li> <li>:ar shows the list of files that are being edited (the vim command-line arguments)</li> <li>:set list shows nonprinting characters</li> <li>:set ic makes searches case-insensitive.</li> <li>:set hls highlights searches (default behavior most of the time)</li> <li>:set noh stops highlighting the given search pattern</li> <li>:set nu shows line numbers</li> <li>:set ff=dos converts EOL format to dos</li> <li>:set ff=unix converts EOL format to unix</li> <li>:g/regexp/d deletes all lines containing regexp</li> <li>:v/regexp/d deletes all lines except those containing regexp</li> <li>:%s/regexp1/regexp2/g substitutes regexp2 for regexp1 every occurrence in the document.</li> <li>:history shows vim history</li> </ul><p>You can also search through history by hitting / or ? or : and using the up and down arrows.</p> <h3 data-id="screen"><span id="screen">screen</span></h3> <p>screen is another great Linux utility. This will allow you to work through multiple windows on a given system. Ctrl-A is the primary meta key that is used for screen.</p> <pre class="code codeBlock" spellcheck="false" tabindex="0"> Ctrl-A, c to create new screen Ctrl-A, :caption always--this gives a caption at the bottom of the screen Ctrl-A, Ctrl-A to flip between windows Ctrl-A, <window #> to go to that window Ctrl-A, n goes to next window Ctrl-A, p goes to previous window Ctrl-A, ' to switch to a given window Ctrl-A, to show all windows Ctrl-A, d to detach screen Ctrl-A, K to kill current window Ctrl-A, :number <#> to change order of screen </pre> <p>exit will exit a screen and show screen is terminating when you have exited the last screen</p> <pre class="code codeBlock" spellcheck="false" tabindex="0"> screen -ls lists screens. if there are multiple, specify the PID for the one you would like to connect to screen -x will allow you to join a current screen session screen -d -r will allow you to detach and reattach a screen </pre> <h2 data-id="parsing-through-text-files"><span id="Parsing_through_Text_Files">Parsing through Text Files</span></h2> <h3 data-id="grep"><span id="grep">grep</span></h3> <p>search files for patterns (regular expressions)</p> <dl><dd>-i ignore case</dd> <dd>-v find all lines except those containing this pattern</dd> <dd>-l list files where matches are found, not individual lines from the files</dd> <dd>-A<#> for context list # of lines after match was found</dd> <dd>-B<#> for context list # of lines before match was found</dd> <dd>-n show line number of file where match was found</dd> <dd>-r recursive--find files in initial directory and all subdirectories</dd> </dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> grep <pattern> <filename> </pre> <p>egrep allows for extended regular expressions and easy searching for multiple patterns</p> <pre class="code codeBlock" spellcheck="false" tabindex="0"> ps aux | egrep 'tasker|devmonitor' root 1035 0.0 0.0 115544 9680 ? SNs Sep29 2:17 /usr/bp/bin/tasker root 18802 0.0 0.0 108800 5216 ? SNs Sep29 13:34 /usr/bp/bin/devmonitor root 30530 0.0 0.0 61188 832 pts/4 S+ 20:09 0:00 egrep tasker|devmonitor </pre> <h3 data-id="sort"><span id="sort">sort</span></h3> <p>sorts lines, usually alphabetically by default using the first field in each line</p> <dl><dd>-u show unique lines only</dd> <dd>-n numeric sort</dd> <dd>-r reverse order</dd> <dd>-k<#> sort by this field number</dd> </dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> cat a.txt | sort -nr -k2 would sort the lines in a.txt numerically in reverse using field 2 </pre> <h3 data-id="sed"><span id="sed">sed</span></h3> <p>stream editor, can be used to replace strings and manipulate files and output streams</p> <dl><dd>-i inline, perform this on a file rather than throwing the output to the screen (standard out)</dd> <dd>to substitute the number 7 for 4 throughout an entire file you would do the following (g is for global rather than the first occurrence per line)</dd> </dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> sed -i a.txt 's/4/7/g' </pre> <h2 data-id="network-commands"><span id="Network_Commands">Network Commands</span></h2> <p>nmap <IP address or host name> will show ports open/closed at a given IP or host name</p> <h2 data-id="processes-and-files"><span id="Processes_and_Files">Processes and Files</span></h2> <h3 data-id="kill"><span id="kill">kill</span></h3> <p>kills a process by process number.</p> <dl><dd>i.e., kill 5900 kills process with PID of 5900.</dd> <dd>the default kill signal is -15 (SIGTERM, which allows process to cleanup), but if all else fails when trying to kill a process, kill -9 (SIGKILL) is a rude way to kill a process.</dd> </dl><h3 data-id="killall"><span id="killall">killall</span></h3> <p>killall is similar to kill but allows you to use a process name, and it will kill multiple processes with that name</p> <h3 data-id="file"><span id="file">file</span></h3> <p>will tell you what kind of data is contained in a file (tar archive, etc.)</p> <dl><dd>just run file <filename></dd></dl><h3 data-id="chmod"><span id="chmod">chmod</span></h3> <p>change permissions on a file</p> <dl><dd>+x will make a file executable.</dd></dl><h3 data-id="chown"><span id="chown">chown</span></h3> <p>change ownership of a file/directory, can point to user and/or group</p> <dl><dd>chown root /backups/a.txt would change ownership of a.txt to root.</dd> <dd>chown -R root:root /home would change ownership of all files in /home and subdirectories to the root user and root group.</dd> </dl><h3 data-id="ln"><span id="ln">ln</span></h3> <p>creates hard links for files or soft (symbolic) links for directories or files</p> <dl><dd>typical format ia for symbolic links is</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> ln -s <target path> <path for link to be created> </pre> <dl><dd>example--you could remove logs.dir and create a symbolic link in its place to a new directory that you had created as /backups/logs.dir</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> ln -s /backups/logs.dir /usr/bp/logs.dir </pre> <h3 data-id="mount"><span id="mount">mount</span></h3> <p>mount drives or network shares, etc.</p> <dl><dd>typical format is</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> mount <target> <mount point> </pre> <dl><dd>i.e.</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> mount /dev/mapper/vgbackup-lvbackup /backups </pre> <p>If a drive is already listed in /etc/fstab, you can often just use the mount point to mount it.</p> <dl><dd>i.e.</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> mount /backups </pre> <h3 data-id="umount"><span id="umount">umount</span></h3> <p>unmount drives, network shares, etc.</p> <pre class="code codeBlock" spellcheck="false" tabindex="0"> umount /backups </pre> <p>Note: If you get an error about files being in use, you may need to cd out of that directory. Also you can use lsof to see what files are open.</p> <h3 data-id="lsof"><span id="lsof">lsof</span></h3> <p>list open files</p> <dl><dd>common usage:</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> lsof | grep <mount point> </pre> <dl><dd> <dl><dd>i.e.</dd></dl></dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> lsof | grep /backups </pre> <h3 data-id="find"><span id="find">find</span></h3> <p>utility for finding files</p> <dl><dd>typical usage is</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> find <what directory to start from> -type <d for directory, f for file, etc.> </pre> <dl><dd>i.e. to find files with a name containing 123 or 456, do the following:</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> find /backups -type f | egrep '123|456' </pre> <h3 data-id="tar"><span id="tar">tar</span></h3> <p>used to create or extract from archives</p> <dl><dd>to create a tarball (tar of gzipped files) of /usr/bp/logs.dir at /tmp/logs.tgz</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> tar -czvf /tmp/logs.tgz /usr/bp/logs.dir </pre> <dl><dd>to extract from a log file (normally extracts as relative paths to the current directory, and will overwrite. BE CAREFUL!)</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> tar -xzvf /tmp/logs.tgz </pre> <dl><dd>view files within an archive</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> tar -tvf /tmp/logs.tgz </pre> <h3 data-id="rpm"><span id="rpm">rpm</span></h3> <p>redhat package manager for installing, upgrading packages, etc. Be very careful with rpm and check with a senior engineer before doing anything!</p> <dl><dd>show all packages on system</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> rpm -qa </pre> <dl><dd>show all unitrends packages in order</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> rpm -qa unitrends* | sort or rpm -qa | grep unitrends </pre> <dl><dd>update packages</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> rpm -Uvh *.rpm </pre> <h3 data-id="yum"><span id="yum">yum</span></h3> <p>easier way to update packages, especially when dependencies are needed etc.</p> <dl><dd>yum list updates shows updates available</dd> <dd>yum update updates all packages with updates available</dd> <dd>yum update unitrends* updates all unitrends packages</dd> <dd>yum localupdate updates packages from a local directory. Sometimes this is a workaround if packages have to be transferred via an unusual route (if WAN access is not available, etc.)</dd> </dl><h2 data-id="file-transfer"><span id="File_Transfer">File Transfer</span></h2> <h3 data-id="wget"><span id="wget">wget</span></h3> <p>download files using ftp</p> <pre class="code codeBlock" spellcheck="false" tabindex="0"> wget <a rel="nofollow" href="/home/leaving?allowTrusted=1&target=ftp%3A%2F%2Fftp.unitrends.com%2Fbp%2Flatest_build%2Fairbag.tar">ftp://ftp.unitrends.com/bp/latest_build/airbag.tar</a> </pre> <dl><dd>show current IP address</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> wget -qO - icanhazip.com </pre> <dl><dd>get multiple files using dash (reads from standard input)</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> wget - <a rel="nofollow" href="/home/leaving?allowTrusted=1&target=ftp%3A%2F%2Fftp.unitrends.com%2Fbp%2Flatest_build%2FWindows%2FPC61_Universal.exe">ftp://ftp.unitrends.com/bp/latest_build/Windows/PC61_Universal.exe</a> <a rel="nofollow" href="/home/leaving?allowTrusted=1&target=ftp%3A%2F%2Fftp.unitrends.com%2Fbp%2Flatest_build%2FWindows%2Fmd5sums">ftp://ftp.unitrends.com/bp/latest_build/Windows/md5sums</a> </pre> <h3 data-id="scp"><span id="scp">scp</span></h3> <p>secure copy protocol, use to securely transfer files using ssh</p> <dl><dd>-P port number</dd> <dd>-C compress</dd> <dd>to transfer abc.txt from a customer DPU to your home directory on the tunnel server. use your username rather than jsnipes</dd> </dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> scp -P 222 -C abc.txt jsnipes@tunnel.unitrends.com:~/ </pre> <dl><dd>to grab output.txt from a remote system and transfer it to current directory</dd></dl><pre class="code codeBlock" spellcheck="false" tabindex="0"> scp -C root@uvault1a:/tmp/output.txt . </pre> <h2 data-id="notes"><strong>NOTES</strong></h2> <p>Commands only</p> </article> </main>