Difference between revisions of "Selected Linux commands"

From Biowikifarm Metawiki
Jump to: navigation, search
(Other commands: +top: interactive taskmanager)
m (Other commands: +highlighting nano)
Line 67: Line 67:
 
== Other commands ==
 
== Other commands ==
  
* <code>less</code> to read, <code>tail</code> to read end of log file, <code>nano</code>: an easier editor on debian 4.
+
* <code>less</code> to read, <code>tail</code> to read end of log file, <code>nano</code>: an easier editor on debian 4 with syntax highlighting set globally in <tt>/etc/nanorc</tt>.
 
* <code>ls -lap</code>  to better see all files, a for hidden, p to see folders marked with trailing "/"
 
* <code>ls -lap</code>  to better see all files, a for hidden, p to see folders marked with trailing "/"
  

Revision as of 18:05, 4 July 2010

Command-line tricks:

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

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

locate memcached

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

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 (packagename) 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 x in *.jpeg; do n=${x/.jpeg/.jpg}; mv $x $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
tar cf - "!" | 7za a -si -mx7 "!.tar.7z"
# tar/7z to folder
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 (?).