Difference between revisions of "Selected Linux commands"

From Biowikifarm Metawiki
Jump to: navigation, search
m (Other commands: +highlighting nano)
m (+color)
Line 1: Line 1:
 +
{{<div style="position:fixed;right:10px;">
 +
__TOC__
 +
</div>
 +
== Tips & first steps ==
 
Command-line tricks:
 
Command-line tricks:
 
* Type Control-R and start typing = will match last command that started that way
 
* Type Control-R and start typing = will match last command that started that way
* Tab will autocomplete, but from all available commands
+
* Tab will autocomplete everything, but from ''all'' available commands
  
 
Most useful to quickly locate commands, executable is "locate", such as:
 
Most useful to quickly locate commands, executable is "locate", such as:
  locate memcached
+
<source lang="bash">
Traditional find files uses:  
+
  locate memcached # or
 +
whereis memcached
 +
apropos ftp # searches for all ftp programs
 +
</source>
 +
 
 +
Traditional find files uses:
 +
<source lang="bash">
 
  find / -name 'file.ext' # "/" to start at root, option -name to search for file names
 
  find / -name 'file.ext' # "/" to start at root, option -name to search for file names
 +
</source>
 
Very useful to find '''software which may not yet be installed''':
 
Very useful to find '''software which may not yet be installed''':
 +
<source lang="bash">
 
  apt-cache search YourSearchTerm
 
  apt-cache search YourSearchTerm
 +
</source>
 +
 +
=== Get help ===
 +
<source lang="bash">
 +
apropos ftp # searches for all program descriptions with ftp
 +
man 7z # manual page for 7z
 +
  # shift + ? → searching a string (by regular expression)
 +
  # shift + n/n → find the next/previous match
 +
  # q → quit
 +
</source>
 +
Most commands provide a help option and the following works:
 +
<source lang="bash">
 +
a-command --help
 +
a-command -h
 +
# sometimes there is an info on usage
 +
a-command --usage
 +
</source>
  
 
== Stop services / demons ==
 
== Stop services / demons ==
  
 +
<source lang="bash">
 
  /etc/init.d/apache2 stop
 
  /etc/init.d/apache2 stop
 
  /etc/init.d/memcached stop
 
  /etc/init.d/memcached stop
Line 17: Line 47:
 
  /etc/init.d/tomcat5.5 stop
 
  /etc/init.d/tomcat5.5 stop
 
  /etc/init.d/webmin stop
 
  /etc/init.d/webmin stop
 +
</source>
  
 
to restart (template to copy and use directly):
 
to restart (template to copy and use directly):
 
+
<source lang="bash">
 
  /etc/init.d/apache2 start
 
  /etc/init.d/apache2 start
 
  # /etc/init.d/memcached start
 
  # /etc/init.d/memcached start
Line 25: Line 56:
 
  /etc/init.d/tomcat5.5 start
 
  /etc/init.d/tomcat5.5 start
 
  /etc/init.d/webmin start
 
  /etc/init.d/webmin start
 +
</source>
  
 
Fedora may or may not be installed under the main tomcat. As of 2009-08, it can be started/stopped using (fedora folder is a softlink to current installed version; alternatively one can use "$FEDORA_HOME"):
 
Fedora may or may not be installed under the main tomcat. As of 2009-08, it can be started/stopped using (fedora folder is a softlink to current installed version; alternatively one can use "$FEDORA_HOME"):
 +
<source lang="bash">
 
  /usr/share/fedora/tomcat/bin/shutdown.sh
 
  /usr/share/fedora/tomcat/bin/shutdown.sh
 
  /usr/share/fedora/tomcat/bin/startup.sh
 
  /usr/share/fedora/tomcat/bin/startup.sh
 +
</source>
  
  
Line 34: Line 68:
  
 
How much space on disks?
 
How much space on disks?
 +
<source lang="bash">
 
  df -l # diskfree, local disks only
 
  df -l # diskfree, local disks only
 
  df -lh # diskfree, human friendly (size in MB, GB, etc.)
 
  df -lh # diskfree, human friendly (size in MB, GB, etc.)
 
  df .  # diskfree current disk
 
  df .  # diskfree current disk
 +
</source>
  
 
Disk usage = "tree size": where is the space used:
 
Disk usage = "tree size": where is the space used:
 +
<source lang="bash">
 
  du -h --max-depth=1 # '''analyze only 1 level of directories deep'''
 
  du -h --max-depth=1 # '''analyze only 1 level of directories deep'''
 
     # -h = human readable, MB, GB, factor 1024; -si would be factor 1000.
 
     # -h = human readable, MB, GB, factor 1024; -si would be factor 1000.
 
  du -S # do not add up content of folders, keep values Separate (useful for manual analysis)
 
  du -S # do not add up content of folders, keep values Separate (useful for manual analysis)
 +
</source>
  
 
For rights, it is often necessary to change the group of a file or folder. It is not necessary to change the owner as well (chown -R).
 
For rights, it is often necessary to change the group of a file or folder. It is not necessary to change the owner as well (chown -R).
 +
<source lang="bash">
 
  ls -l    # will display owner and group names
 
  ls -l    # will display owner and group names
 
  ls -g    # will only display group names (easier)
 
  ls -g    # will only display group names (easier)
 
  chgrp -R # R: do it recursively
 
  chgrp -R # R: do it recursively
 +
</source>
  
 
When copying folder trees, it is easy to loose essential information. Use
 
When copying folder trees, it is easy to loose essential information. Use
 +
<source lang="bash">
 
  cp -pr  # preserve owner, rights, etc., copy recursively;  
 
  cp -pr  # preserve owner, rights, etc., copy recursively;  
 
  # but devices, sockets, etc. still are not handled, if this is necessary use:
 
  # but devices, sockets, etc. still are not handled, if this is necessary use:
 
  tar -p
 
  tar -p
 +
</source>
  
 
== Searching ==
 
== Searching ==
Line 57: Line 99:
 
Locate does not work, and using Find is tough. Good info: http://content.hccfl.edu/pollock/unix/findcmd.htm Example for find:
 
Locate does not work, and using Find is tough. Good info: http://content.hccfl.edu/pollock/unix/findcmd.htm Example for find:
  
 +
<source lang="bash">
 
   find / -name index.html
 
   find / -name index.html
 +
</source>
  
 
For searching inside files, use grep (-r searches recursive, but only within the file pattern, grep -r "x" index.html would not work!):
 
For searching inside files, use grep (-r searches recursive, but only within the file pattern, grep -r "x" index.html would not work!):
  
 +
<source lang="bash">
 
  grep -r "<script>" *
 
  grep -r "<script>" *
 
+
</source>
 
+
  
 
== Other commands ==
 
== Other commands ==
Line 88: Line 132:
 
** <code>apt-get clean</code> will clear the repository completely
 
** <code>apt-get clean</code> will clear the repository completely
  
* <code>dpkg -l</code> to list all packages with codes for status, e.g.'ii' for installed,'rc' for removed, but configuration files still there; <code>dpkg -s (packagename)</code> to get information on the status of a package
+
* <code>dpkg -l</code> to list all packages with codes for status, e.g.'ii' for installed,'rc' for removed, but configuration files still there; <code>dpkg -s (package name)</code> to get information on the status of a package
 
* <code>cat -v filename</code> to display non-printing characters so they are visible. If the file has been edited on a Windows machine it can sometimes Add CR/LF (VM) characters on the end of each line (hidden by default on most editors), so #!/bin/sh becomes #!/bin/shVM. This causes error: bad interpreter ^M. To remove such characters, use e.g. <code>cat infilename | tr -d "\r" > outfilename</code>  
 
* <code>cat -v filename</code> to display non-printing characters so they are visible. If the file has been edited on a Windows machine it can sometimes Add CR/LF (VM) characters on the end of each line (hidden by default on most editors), so #!/bin/sh becomes #!/bin/shVM. This causes error: bad interpreter ^M. To remove such characters, use e.g. <code>cat infilename | tr -d "\r" > outfilename</code>  
  
Line 124: Line 168:
 
== Renaming file extensions ==
 
== Renaming file extensions ==
  
Linux does not support wildcards in the target of a move command the way Windows does. The equivalent for  
+
Linux does not support wildcards in the target of a move command the way Windows does. The equivalent for Windows:  
Windows: rename *.jpeg *.jpg
+
rename *.jpeg *.jpg
 
is
 
is
for x in *.jpeg; do n=${x/.jpeg/.jpg}; mv $x $n; done
+
<source lang="bash">
 +
for files in *.jpeg;  
 +
  do n=${files/.jpeg/.jpg}; # saves to variable $n
 +
  mv $files $n;  
 +
done
 +
</source>
  
 
Related: To add a prefix use:
 
Related: To add a prefix use:
 +
<source lang="bash">
 
  for i in *.jpg; do mv -i "$i" "XXX_$i"; done
 
  for i in *.jpg; do mv -i "$i" "XXX_$i"; done
 +
</source>
  
  
Line 136: Line 187:
  
 
With the general zip, p7zip-full etc. installed, the following commands work:
 
With the general zip, p7zip-full etc. installed, the following commands work:
 
+
<source lang="bash">
 
  # -9 is optional, higher compression
 
  # -9 is optional, higher compression
 
  zip archivename.zip file.sql /folder -9   
 
  zip archivename.zip file.sql /folder -9   
Line 144: Line 195:
 
  7z a archivename.7z file.sql /folder
 
  7z a archivename.7z file.sql /folder
 
  7z x archivename.7z
 
  7z x archivename.7z
 +
</source>
  
 
Note: Because 7z will not store owner or group information,  the option -r = recurse into subfolders is not recommended. To archive folders use (where ! is the folder name, as in WinSCP custom commands):
 
Note: Because 7z will not store owner or group information,  the option -r = recurse into subfolders is not recommended. To archive folders use (where ! is the folder name, as in WinSCP custom commands):
  # tar-7z a folder
+
<source lang="bash">
 +
  # tar + 7z a folder: cf = create file, a = add, -si = Read data from StdIn
 
  tar cf - "!" | 7za a -si -mx7 "!.tar.7z"
 
  tar cf - "!" | 7za a -si -mx7 "!.tar.7z"
  # tar/7z to folder
+
  # tar/7z to folder: x = eXtract with full paths, -so = Write data to StdOut, bd = Disable percentage indicator
 +
# xf = extract file
 
  7za x -so -bd "!" | tar xf -
 
  7za x -so -bd "!" | tar xf -
 
+
</source>
 
Note: inside WinSCP, the tar/7z command to folder results in error (ok in ssh), but simply selecting SKIP results in correct result, this seems to be more a bug of the way WinSCP handles messages than of the process (?).
 
Note: inside WinSCP, the tar/7z command to folder results in error (ok in ssh), but simply selecting SKIP results in correct result, this seems to be more a bug of the way WinSCP handles messages than of the process (?).
  
 
[[Category: Software documentation]]
 
[[Category: Software documentation]]

Revision as of 15:40, 7 July 2010

{{

Tips & first steps

Command-line tricks:

  • Type Control-R and start typing = will match last command that started that way
  • Tab will autocomplete everything, but from all available commands

Most useful to quickly locate commands, executable is "locate", such as:

 locate memcached # or
 whereis memcached
 apropos ftp # searches for all ftp programs

Traditional find files uses:

 find / -name 'file.ext' # "/" to start at root, option -name to search for file names

Very useful to find software which may not yet be installed:

 apt-cache search YourSearchTerm

Get help

 apropos ftp # searches for all program descriptions with ftp
 man 7z # manual page for 7z
   # shift + ? → searching a string (by regular expression)
   # shift + n/n → find the next/previous match
   # q → quit

Most commands provide a help option and the following works:

 a-command --help
 a-command -h
 # sometimes there is an info on usage
 a-command --usage

Stop services / demons

 /etc/init.d/apache2 stop
 /etc/init.d/memcached stop
 /etc/init.d/mysql stop
 /etc/init.d/tomcat5.5 stop
 /etc/init.d/webmin stop

to restart (template to copy and use directly):

 /etc/init.d/apache2 start
 # /etc/init.d/memcached start
 /etc/init.d/mysql start
 /etc/init.d/tomcat5.5 start
 /etc/init.d/webmin start

Fedora may or may not be installed under the main tomcat. As of 2009-08, it can be started/stopped using (fedora folder is a softlink to current installed version; alternatively one can use "$FEDORA_HOME"):

 /usr/share/fedora/tomcat/bin/shutdown.sh
 /usr/share/fedora/tomcat/bin/startup.sh


Disk usage and rights

How much space on disks?

 df -l # diskfree, local disks only
 df -lh # diskfree, human friendly (size in MB, GB, etc.)
 df .  # diskfree current disk

Disk usage = "tree size": where is the space used:

 du -h --max-depth=1 # '''analyze only 1 level of directories deep'''
     # -h = human readable, MB, GB, factor 1024; -si would be factor 1000.
 du -S # do not add up content of folders, keep values Separate (useful for manual analysis)

For rights, it is often necessary to change the group of a file or folder. It is not necessary to change the owner as well (chown -R).

 ls -l    # will display owner and group names
 ls -g    # will only display group names (easier)
 chgrp -R # R: do it recursively

When copying folder trees, it is easy to loose essential information. Use

 cp -pr  # preserve owner, rights, etc., copy recursively; 
 # but devices, sockets, etc. still are not handled, if this is necessary use:
 tar -p

Searching

Locate does not work, and using Find is tough. Good info: http://content.hccfl.edu/pollock/unix/findcmd.htm Example for find:

  find / -name index.html

For searching inside files, use grep (-r searches recursive, but only within the file pattern, grep -r "x" index.html would not work!):

 grep -r "<script>" *

Other commands

  • less to read, tail to read end of log file, nano: an easier editor on debian 4 with syntax highlighting set globally in /etc/nanorc.
  • ls -lap to better see all files, a for hidden, p to see folders marked with trailing "/"
  • cat /etc/passwd to see user list, cat = list, here: the password file
  • adduser, deluser, passwd more on user management: http://www.cae.wisc.edu/site/public/?title=linaccounts
  • "taskmanager": ps -ef list all processes, including services, use kill (number) or pkill (name)
  • "taskmanager" interactive: top or top u root list all processes; u switches the user; Shift+F change sorting column; > or < switches sorting to → or ←; Shift+R alters descending or ascending column sorting; k kill; c show command paths instead of process names; q quit
  • kill processes by name: for example killall memcached

vnc = special vnc user, not sure whether useful.

A source helping to understand the Linux file system is: http://www.pathname.com/fhs/pub/fhs-2.3.html

Install commands

  • To find package names use:
    • aptitude search (keyword)
    • apt-cache search (keyword)
  • Clean-up:
    • apt-get autoremove will remove packages no longer needed.
    • apt-get clean will clear the repository completely
  • dpkg -l to list all packages with codes for status, e.g.'ii' for installed,'rc' for removed, but configuration files still there; dpkg -s (package name) to get information on the status of a package
  • cat -v filename to display non-printing characters so they are visible. If the file has been edited on a Windows machine it can sometimes Add CR/LF (VM) characters on the end of each line (hidden by default on most editors), so #!/bin/sh becomes #!/bin/shVM. This causes error: bad interpreter ^M. To remove such characters, use e.g. cat infilename | tr -d "\r" > outfilename

ls colors

Putty displays ls out in colors. These are:

  • Executable files: Green
  • Normal file : Normal
  • Directory: Blue
  • Symbolic link : Cyan
  • Pipe: Yellow
  • Socket: Magenta
  • Block device driver: Bold yellow foreground, with black background
  • Character device driver: Bold yellow foreground, with black background
  • Orphaned syminks : Blinking Bold white with red background
  • Missing links ( - and the files they point to) : Blinking Bold white with red background
  • Archives or compressed : Red (.tar, .gz, .zip, .rpm)
  • Image files : Magenta (.jpg, gif, bmp, png, tif)

OR (other source):

Type                Foreground Background
Folder/Directory    blue      (default)
Symlink             magenta   (default)
Socket              green     (default)
Pipe                brown     (default)
Executable          red       (default)
Block               blue      cyan
Character           blue      brown
Exec. w/ SUID       black     red
Exec. w/ SGID       black     cyan
Dir, o+w, sticky    black     green
Dir, o+w, unsticky  black     brown

Renaming file extensions

Linux does not support wildcards in the target of a move command the way Windows does. The equivalent for Windows:

rename *.jpeg *.jpg

is

for files in *.jpeg; 
   do n=${files/.jpeg/.jpg}; # saves to variable $n
   mv $files $n; 
done

Related: To add a prefix use:

 for i in *.jpg; do mv -i "$i" "XXX_$i"; done


Archiving

With the general zip, p7zip-full etc. installed, the following commands work:

 # -9 is optional, higher compression
 zip archivename.zip file.sql /folder -9  
 unzip archivename.zip
 # for real good compression use:
 # (a = add, -mx7 and -mx9 = higher compression, x = extract)
 7z a archivename.7z file.sql /folder
 7z x archivename.7z

Note: Because 7z will not store owner or group information, the option -r = recurse into subfolders is not recommended. To archive folders use (where ! is the folder name, as in WinSCP custom commands):

 # tar + 7z a folder: cf = create file, a = add, -si = Read data from StdIn
 tar cf - "!" | 7za a -si -mx7 "!.tar.7z"
 # tar/7z to folder: x = eXtract with full paths, -so = Write data to StdOut, bd = Disable percentage indicator
 # xf = extract file
 7za x -so -bd "!" | tar xf -

Note: inside WinSCP, the tar/7z command to folder results in error (ok in ssh), but simply selecting SKIP results in correct result, this seems to be more a bug of the way WinSCP handles messages than of the process (?).