Difference between revisions of "Linux commands"

From KnowledgeNerd
Jump to navigation Jump to search
 
(8 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
== Basic commands ==
 
== Basic commands ==
  
−
=== Getting Help ===
+
=== Getting help ===
  
 
<pre>
 
<pre>
Line 31: Line 31:
 
</pre>
 
</pre>
  
−
=== Managing Files And Directories ===
+
=== Managing files and directories ===
  
 
==== Copy ====
 
==== Copy ====
Line 64: Line 64:
 
</pre>
 
</pre>
  
−
==== Move and Rename ====
+
==== Move and rename ====
  
 
<pre>
 
<pre>
Line 89: Line 89:
 
</pre>
 
</pre>
  
−
=== Comparing Files ===
+
=== Comparing files ===
  
 
<pre>
 
<pre>
Line 101: Line 101:
 
* [[Windows#Diff_Tool_In_Windows | Diff Tool In Windows]]
 
* [[Windows#Diff_Tool_In_Windows | Diff Tool In Windows]]
  
−
=== Symbolic Links ===
+
=== Symbolic links ===
  
 
<pre>
 
<pre>
Line 109: Line 109:
 
</pre>
 
</pre>
  
−
=== Listing, Finding and Searching Inside Files ===
+
=== Listing, finding and searching inside files ===
  
 
<pre>
 
<pre>
Line 182: Line 182:
 
exclude dirs after change dir ............ $tar -czf drupal-6.tar.gz -C '/Users/john/websites/' --exclude 'drupal-6/resources' --exclude 'drupal-6/forum/files' drupal-6/
 
exclude dirs after change dir ............ $tar -czf drupal-6.tar.gz -C '/Users/john/websites/' --exclude 'drupal-6/resources' --exclude 'drupal-6/forum/files' drupal-6/
 
compress files with gzip (.gz) ........... $gzip filename.foo (it REPLACES your original file with a compressed version: filename.foo.gz)
 
compress files with gzip (.gz) ........... $gzip filename.foo (it REPLACES your original file with a compressed version: filename.foo.gz)
−
compress files with gzip (.gz) ........... $gzip -k filename.foo (it creates compressed version: filename.foo.gz and keeps your original file also. works only in gzip versions since June 2013.)
+
compress files with gzip (.gz) ........... $gzip -k filename.foo (it creates a compressed version: filename.foo.gz and keeps your original file also. works only in gzip versions since June 2013.)
 +
compress files with gzip (.gz) ........... $gzip -c filename.foo > filename.foo.gz (it creates a compressed version: filename.foo.gz and keeps your original file also.)
 
compress folder with gzip ................ $make a .tar file first and then gzip or use -r to make multiple .gz files
 
compress folder with gzip ................ $make a .tar file first and then gzip or use -r to make multiple .gz files
 
make a .tar file of folder contents ...... $tar -cvvf d6_bu.tar /var/www/drupal6
 
make a .tar file of folder contents ...... $tar -cvvf d6_bu.tar /var/www/drupal6
Line 209: Line 210:
 
</pre>
 
</pre>
  
−
==== List Files In Archives ====
+
==== List files in archives ====
  
 
<pre>
 
<pre>
Line 283: Line 284:
 
</pre>
 
</pre>
  
−
==== OS X Specific Permissions ====
+
==== OS X specific permissions ====
  
 
<pre>
 
<pre>
Line 358: Line 359:
 
</pre>
 
</pre>
  
−
=== Date and Time ===
+
=== Date and time ===
  
 
<pre>
 
<pre>
Line 374: Line 375:
 
http://codeghar.wordpress.com/2007/12/06/manage-time-in-ubuntu-through-command-line/
 
http://codeghar.wordpress.com/2007/12/06/manage-time-in-ubuntu-through-command-line/
  
−
=== Job Managing ===
+
=== Job managing ===
  
 
Job and task handling tips
 
Job and task handling tips
Line 394: Line 395:
 
The job is now running in the background
 
The job is now running in the background
  
−
== Administration Related Commands ==
+
== Administration related commands ==
  
−
=== Operating System Restart / Reboot / Shut Down ===
+
=== Operating system restart / reboot / shut down ===
  
 
<pre>
 
<pre>
Line 416: Line 417:
 
* [[Crontab|Crontab tips]]
 
* [[Crontab|Crontab tips]]
  
−
=== Process Handling ===
+
=== Process handling ===
  
−
==== Show Processes ====
+
==== Show processes ====
 
<pre>
 
<pre>
 
display all running processes ............ $ps -A (ps stands for "process status")
 
display all running processes ............ $ps -A (ps stands for "process status")
Line 432: Line 433:
 
</pre>
 
</pre>
  
−
==== Kill Processes ====
+
==== Kill processes ====
 
<pre>
 
<pre>
 
terminate process with id 3486 ........... $kill 3486
 
terminate process with id 3486 ........... $kill 3486
Line 440: Line 441:
 
</pre>
 
</pre>
  
−
=== Server Related Commands ===
+
=== Server related commands ===
  
 
<pre>
 
<pre>
Line 447: Line 448:
 
</pre>
 
</pre>
  
−
== Network Related Commands ==
+
== Network related commands ==
  
 
* http://www.comptechdoc.org/os/linux/commands/linux_crnetman.html
 
* http://www.comptechdoc.org/os/linux/commands/linux_crnetman.html
  
−
=== Network Interface Commands ===
+
=== Network interface commands ===
  
 
<pre>
 
<pre>
Line 564: Line 565:
 
Copy the files "foo.txt" and "bar.txt" from the remote host to current local directory
 
Copy the files "foo.txt" and "bar.txt" from the remote host to current local directory
 
  $ scp exampleuser@iporhostname.com:"/dir1/foo.txt /dir2/bar.txt" .
 
  $ scp exampleuser@iporhostname.com:"/dir1/foo.txt /dir2/bar.txt" .
 +
 +
Copy the files "foo.txt", "bar.txt" & "baz.txt" from the remote host to current local directory
 +
$ scp exampleuser@iporhostname.com:/dir\{foo.txt,bar.txt,baz.txt\} .
  
 
Copy the file "foobar.txt" from the local host to a remote host using port 2264
 
Copy the file "foobar.txt" from the local host to a remote host using port 2264
Line 569: Line 573:
  
 
Copy file from remote server using port 341 to local users home folder's /Sites/bu subdirectory
 
Copy file from remote server using port 341 to local users home folder's /Sites/bu subdirectory
−
  $ scp -P 341 exampleuser@65.39.183.128:/home/exampleuser/drupal6_bu_20130130.sql.gz ~/Sites/bu
+
  $ scp -P 341 exampleuser@xxx.xxx.xxx.xxx:/home/exampleuser/drupal6_bu_20130130.sql.gz ~/Sites/bu
  
 
=== Different commands ===
 
=== Different commands ===

Latest revision as of 15:20, 25 March 2025

See also: Linux_tips

  • Note! Use sudo if you need administer rights to execute the task.

Basic commands

Getting help

print mysqldump man page (manual) ........ $man mysqldump
print mysqldump help file on the screen .. $mysqldump -?
print mysqldump help file on the screen .. $mysqldump --help
search text from mysqldump help file ..... $mysqldump --help | grep 'textToSearch'
write mysqldump help output into a file .. $mysqldump --help > mysqldumphelp.txt
checking tar-program version ............. $tar --version
searching "version" text from binaries ... $strings /usr/bin/tar | grep -i 'version'

Navigation

change directory ........................... $cd [directory_name]
parent directory ........................... $cd ..
your profile's home directory .............. $cd ~
your profile's desktop directory ........... $cd ~/Desktop
root directory ............................. $cd /
show current absolute path / directory ..... $pwd (e.g. /Users/exampleuser/Desktop)
show physical current working directory .... $pwd -P (all symbolic links resolved)
move to the start of the line .............. $CTRL+a
move to the end of the line ................ $CTRL+e

Managing files and directories

Copy

copy dir contents (when in target dir) ..... $cp -r /etc/examplefolder/* ./
copy dir contents preserving ownership ..... $cp -pr /etc/examplefolder/* ./ (when in target dir. by default -p preserves at least attributes: rwx-rights, ownership, mode and timestamps)
copy dir contents (don't overwrite) ........ $yes n | cp -r -i /etc/examplefolder/* ./ 2>/dev/null (when in target dir. -i switch asks "do you wan't to overwrite" and piped "yes n" command answers automatically "no" to every question)
copy dir contents (don't overwrite) ........ $cp -r -n /etc/examplefolder/* ./ (Note! when in target dir. -n, --no-clobber : do not overwrite an existing files. Note: the option isn't available in all linux version's cp tools!)
COPY, DON'T OVERWRITE, PRESERVE RIGHTS ..... $yes n | cp -pr -i /etc/examplefolder/* ./ 2>/dev/null (when in target dir. 2>/dev/null hides messages.)
copy file .................................. $cp php.ini-dist /usr/local/lib/php.ini
copy multiple files ........................ $cp .htaccess robots.txt /var/www/drupal-6.17/
copy all files starting test ............... $cp test* /var/www/
copy directory (when in source dir) ........ $cp -r examplefolder /var/www/
copy directory (when in target dir) ........ $cp -r /etc/examplefolder ./
copy only if source files are newer ........ $cp -r -u /etc/examplefolder/* ./

Delete

delete/remove file ......................... $rm foo.txt
delete/remove files ........................ $rm foo.txt foo2.txt
delete all files from current directory .... $rm *
delete all files with prompt option ........ $rm -i *  (asks remove file-xx.foo y/n with every file)
delete all except files starting f ......... $rm [!f]*
delete all except specific file ............ $rm $(ls * | grep -v example_file)
delete all except files foo1 & foo2 ........ $rm $(ls | grep -v 'index.htm' | grep -v 'data_hooks.php')
delete empty directory ..................... $rmdir foo
delete nested empty directories ............ $rmdir foo1/foo2/foo3 (same as $rmdir foo1/foo2/foo3 foo1/foo2 foo1)
delete directory which has content ......... $rm -rf foo (remove a directory with no prompts)

Move and rename

move a directory ........................... $mv targetdirectory/ /destination/parent/
move all content of a directory ............ $mv /var/www/drupal/* /var/www/drupal_old (this doesn't move hidden files e.g. .htaccess from top folder: drupal)
move hidden and other files to parent dir .. $mv * .[^.]* ..  (note: .[^.]* means all files starting with a dot, having other character something else than a dot, followed by other arbitrary characters)
move file with same name ................... $mv phpBB3.tar.bz2 /var/www/
move and rename file ....................... $mv phpBB3.tar.bz2 /var/www/phpBB3_bu.tar.bz2
rename file (or folder) .................... $mv foo.txt foo_old.txt

Other

create a directory ......................... $mkdir foo
sets the current time/date for file ........ $settime myfile.txt
create or change accessed time if exists ... $touch newfile.txt
protect a file from deleting it ............ $chattr +i foo.txt (might require sudo. +i is to set the immutable bit to prevent even root from erasing or changing the contents of a file)
protect a dir recursively from deleting .... $chattr -R +i foo (might require sudo in front of the command)
remove write protection from file .......... $chattr -i foo.txt
remove write protection from dir rec ....... $chattr -R -i foo/
hide dir or file (only from GUI view) ...... $chflags hidden foodir / chflags hidden foo.txt (works in BSD & OS X, but don't provide security. Rather use password-protected/encrypted disk images)
hide dir or file ........................... $mv foodir .foodir / mv foo.txt .foo.txt (files and directories starting with dots "." are hidden and you have to use "ls -a" to see them)

Comparing files

compare file contents with ............... $diff file1 file2
compare directory contents ............... $diff folder1 folder2
recursively compare any content (=subds) . $diff -r folder1 folder2
rec compare and print results in file .... $diff -r drupal6/modules_orig/ drupal6/modules/ > system_module_diffs.txt

Symbolic links

create a symbolic link "drupal.php" ...... $ln -s /var/www/drupal6/index.php drupal.php
create a symbolic link "/logs" ........... $ln -s /var/log /logs
remove a symbolic link (only the link) ... $rm /logs

Listing, finding and searching inside files

list files ................................. $ls
list php files in current dir .............. $ls *.php
list php files in specific dir ............. $ls examplefolder/*.php  or  $find system/ -name '*.php'
list files (+hidden+permissions)............ $ls -la
list files order by size desc .............. $ls -lh -S
list files order by size asc ............... $ls -lh -S -r
list files order by modified date desc ..... $ls -lTt (l = show details, T = show complete date/time information, t = sort by modified date, newest first)
list files order by modified date asc ...... $ls -lTtr (r = reverse = oldest files first)
show files modified date (year,month,time) . $ls -lT (output e.g. -rw-r--r--@  1 theUser  staff  125954 Mar 19 19:29:22 2012 foo.txt)
list directories/folders only .............. $ls -d */
list folders only (+hidden+permissions) .... $ls -la -d */

find file from root ........................ $find / -name httpd
find file foo.php starting from curr dir ... $find . -name foo.php
list also file(s) info, e.g. size, mod ..... $find . -name foo.php -ls
find file or folder containing foo* ........ $find -name foo*
find file containing *html* ................ $find -name *html*.*
find all files with .rej extension ......... $find -name *.rej (starting from the current directory. use quotes "*.rej" or go one dir up if you get following message: "find: paths must precede expression: ..."
find *.rej from /foo/diffs & subdirs ....... $find /foo/diffs/ -name *.rej (you probably can't be in the same dir when making the search)
paths must precede expression: viewforum.php.rej")
show all folder ............................ $find . -type d
find file which is in /etc dir ............. $find /etc -name apache2.conf

search text from file, show line# .......... $grep 'exampletext' -n /foo/bar.txt
from all files and sub directories ......... $grep 'exampletext' -n --directories=recurse folder/*
from all files and sub directories ......... $grep 'exampletext' -r /var/www/html/*
from this & subdirs, only filenames ........ $grep 'exampletext' -r -l *
from this & subdirs, ignore case ........... $grep 'ExAmPlEtExT' -r -i *
exclude directory from search .............. $grep 'exampletext' * -rl --exclude-dir=cache
exclude multiple directories from search ... $grep -r 'exampletext' * --exclude-dir=forum --exclude-dir=cache --exclude-dir=files
exclude files from search .................. $grep 'exampletext' -rl --exclude=*.{png,jpg} *
search recursive and ignore binary files ... $grep 'exampletext' -r -I *
to search e.g. '-A' use escape character ... $grep '\-A' /foo/bar.txt

find specific programs files ............... $locate mysqldump
locate patch tool .......................... $whereis patch
locate patch, prints e.g. /usr/bin/patch ... $which patch
find files modified during xx days ......... $find . -mtime -40 | less (exit less: q or Q or :q or :Q or ZZ)
tree view (apt-get install tree) ........... $tree
tree view of only folders .................. $tree -d

Showing file content on screen

print all file contents on screen ........ $cat index.html
show file content ........................ $more index.html (exit: q)
show file content ........................ $less index.html (exit: q, go to the end of file: shift + G)
show first 10 lines of the file .......... $head index.html
show first 50 lines of the file .......... $head -50 index.html
show last 10 lines of the file ........... $tail index.html
show last 50 lines of the file ........... $tail -50 index.html
search all .txt files and unite contents . $find /foo/bar/ -name *.txt -exec cat {} > texts.txt \; (copies all the contents of .txt files to texts.txt)

Packing and unpacking

Compress

compress folder to .tar.gz file .......... $tar -czf d6_bu.tar.gz drupal6/ (archive contains drupal6/ folder. c = create, z = compress archive with gzip, f = filename)
compress folder to .tar.gz file .......... $tar -czf ~/Backups/d6_bu-$(date +%Y-%m-%d-%H.%M.%S).tar.gz drupal6/  (creates a file like d6_bu-2014-01-24-18.05.16.tar.gz)
compress all in cur dir to .tar.gz file .. $tar -c --gzip -f /home/foo/d6_bu.tar.gz * (no parent directories/path in archive, only dirs inside the current dir. remember to specify the target folder when uncompressing the archive)
compress all in cur dir to .tar.gz file .. $tar -czf /home/foo/d6_bu.tar.gz * (no parent directories/path in archive, only dirs inside the current dir. remember to specify the target folder when uncompressing the archive)
compress folder to .tar.gz file .......... $tar -c --gzip -f d6_bu.tar.gz drupal6/* (archive contains drupal6/ folder)
BAD example for .tar.gz file ............. $tar -c --gzip -f d6_bu.tar.gz /Users/john/Sites/drupal6/ (all files in the archive have a path /Users/john/Sites/drupal6/ when uncompressed starting from the current directory where you uncompressed it)
BETTER example for .tar.gz file .......... $tar -c --gzip -f d6_bu.tar.gz -C /Users/john/Sites/ drupal6/ (-C changes the directory first to /Users/john/Sites/ and then compresses drupal6/ dir)
exclude dirs when compressing ............ $tar -czf drupal-6.tar.gz --exclude 'drupal-6/resources' --exclude 'drupal-6/forum/files'  drupal-6/
exclude dirs after change dir ............ $tar -czf drupal-6.tar.gz -C '/Users/john/websites/' --exclude 'drupal-6/resources' --exclude 'drupal-6/forum/files' drupal-6/
compress files with gzip (.gz) ........... $gzip filename.foo (it REPLACES your original file with a compressed version: filename.foo.gz)
compress files with gzip (.gz) ........... $gzip -k filename.foo (it creates a compressed version: filename.foo.gz and keeps your original file also. works only in gzip versions since June 2013.)
compress files with gzip (.gz) ........... $gzip -c filename.foo > filename.foo.gz (it creates a compressed version: filename.foo.gz and keeps your original file also.)
compress folder with gzip ................ $make a .tar file first and then gzip or use -r to make multiple .gz files
make a .tar file of folder contents ...... $tar -cvvf d6_bu.tar /var/www/drupal6
compress to .tar.bz2 file ................ $tar -c --bzip2 -f d6_bu.tar.bz2 drupal6/
compress .zip file ....................... $zip files.zip *
compress specific files to .zip file ..... $zip files.zip file1 file2 file3

Unpack

unpack .tar.gz archive ................... $tar -xzf filename.tar.gz (if same files exists in unpacking destination those will be OVERWRITTEN)
unpack .tar.gz archive ................... $tar -xzvkf filename.tar.gz (k = do not overwrite existing files. --keep-newer-files option do not overwrite existing files that are newer than the versions appearing in the archive being extracted.)
unpack .tar.gz archive ................... $tar -xzvf filename.tar.gz (x = extract, z = gzip, v = verbosely list files processed, f = use archive file)
unpack .tar.gz to specific dir ........... $tar -xzvf drupal-6.17.tar.gz -C /var/www/ (C = change to directory DIR, the directory have to exist or otherwise you'll get an error message)
unpack .tar files ........................ $tar xvf filename.tar
unpack .gz files ......................... $gunzip filename.foo.gz (it REPLACES your original file: filename.foo.gz with an uncompressed version: filename.foo)
unpack .gz files keeping original file ... $gunzip -k filename.foo.gz (keeps original file also. works only in gzip versions since June 2013.)
unpack bzip2 archive ..................... $sudo tar xfj phpBB-3.0.6.tar.bz2
unpack .zip files ........................ $unzip filename.zip
unpack .zip files to specific folder ..... $unzip filename.zip -d /var/www/
unpack .zip (do not make directories) .... $unzip -j filename.zip
unpack lzipped-file (.lz) ................ $lzip -d node_revisions.sql.lz (note! the .lz file is removed if the operation is successful!)
unpack .lz file, keeping orig file also .. $lzip -d --keep node_revisions.sql.lz
unpack stripping the 1st dir level ....... $tar -xzvf /path/to/file.tar.gz --strip 1  (if the archive contains dir structure "parent/children/[files]" it will be unarchived as "children/[files]")

List files in archives

list files in .tar.gz archive ............ $tar -tzf backup.tar.gz (t = list files, z= filter through gzip, f = use archive file)
list files in .gz archive ................ $gunzip -l filename.gz
list files in .zip archive ............... $unzip -l filename.zip

Checking disc, file and folder sizes and file count

check the size of mounted sections (KB).... $df
check the size of mounted sections (MB&GB). $df -h
check the size of specific directory ...... $du -hs /var/www/drupal/
check the size of directories and files ... $du -hs /var/www/drupal/*  (lists size of the files individually)
check the size of dir's subdirs ........... $du -h /var/www/drupal/    (shows a total size)
check the size of dir's files ............. $ls -lh /var/www/drupal/
show all files order by size desc ......... $ls -lh -S
show all files order by size asc .......... $ls -lh -S -r
check the number of files in directory .... $ls -l drupal6 | wc -l

Config and build from source code

e.g. Apache web server:
navigate to the right folder ............. $cd httpd-2_x_xx
configure ................................ $./configure --enable-so [other options]
make/build ............................... $make
install Apache ........................... $make install

If install fails you can try to build it again:
clean old build/configuration ............ $clean make
list available flags ..................... $./configure --help
..and then configure + make/build + install

File and folder permissions & ownership

read & write for current usr ............. $chmod 644 filename.ext
read & write for web server .............. $chmod 666 filename.ext
change folder rights recursively ......... $chmod 750 -R /var/www/drupal
change owner of a file ................... $chown root filename.ext
change owner and group of a file ......... $chown root.www-data filename.ext
change owner and group of a file ......... $chown root:www-data filename.ext
change owner & group of a dir&files rec .. $chown root:www-data folder/ -R
change o & g of all files in current dir . $chown www-data:www-data *
change only a owner group ................ $chown :www-data config.php
change only a owner group ................ $chgrp www-data config.php
change owner group recursively (fil&dir) . $chgrp -R www-data examplefolder
remove write perm from current group ..... $chmod g-w examplefolder
add execute perm for file owner (rec) .... $chmod u+x -R examplefolder
add write access to others group (002) ... $chmod o+w drupal-7/sites/default/settings.php
change all root right to theuser rec ..... $chown -R --from=root:root theuser:theuser * (starting from current dir, change all file ownership which are owned by root to theuser)

e.g. files folder permissions
chmod 754 files
1. number = u = user/owner
2. number = g = group
3. number = o/a = others group / anonymous users

ls -la
drwxr-xr-- 2 1080 www-data 4096 2010-06-13 10:12 files

d = type: directory/folder
r = read = 4
w = write = 2
x = execute = 1
- = no permission = 0

permission numbers are calculated from binaries e.g. full access 7 = 4+2+1 = rwx

OS X specific permissions

other permission examples:
-rw-r--r--@
"@" symbol appearing to the right of the permission tables (OS X specific) means the file has additional attributes which you can see by typing: xattr -l foo.txt --> com.apple.quarantine: 0001;5264fbed;Firefox;)

list all files and their attributes ...... $ls -la@

xattr
list all attributes in current directory . $xattr *
list additional file attributes .......... $xattr foo.txt --> com.apple.quarantine
list detailed additional file attributes . $xattr -l foo.txt --> com.apple.quarantine: 0001;5264fbed;Firefox;
delete one extended attribute ............ $xattr -d com.apple.quarantine foo.txt
remove all extended attributes ........... $xattr -c foo.txt

-rw-r--r--+
This indicates the file has an ACL, short for Access Control List, which is used to give fine grained control over file permissions (OS X specific), beyond what is available with the regular unix permission tables.
Typing the following will show these additional permissions for files in a directory: ls -le

drwxr-sr-x
s = 

User management

add a new user ........................... $adduser exampleuser
delete a user account and related files .. $userdel exampleuser
logout user .............................. $skill -KILL -u exampleuser
list all users and their home folders .... $cat /etc/passwd
show all users which has /home directory . $cat /etc/passwd |grep "/home" |cut -d: -f1
change user's home directory ............. $usermod -d /exampledir/subdir exampleuser
change password for current users ........ $passwd
change password for specific users ....... $passwd exampleuser
change password for group ................ $passwd -g examplegroup
enable Ubuntu root account ............... $sudo passwd root
change user .............................. $su exampleuser

Group management

create a new group ....................... $groupadd examplegroup
delete group ............................. $groupdel examplegroup
show all groups .......................... $less /etc/group
show the current user's groups ........... $groups
show groups of a specific user ........... $groups exampleuser
List all members of a group .............. grep 'examplegroup' /etc/group
List all members of a group .............. members examplegroup (apt install members)
List all members of a group .............. lid -g examplegroup (apt install libuser)
add user to a new group (keep old grps) .. $usermod -a -G examplegroup exampleuser
add user to a new group (keep old grps) .. $adduser exampleuser examplegroup
add user to multiple groups .............. $usermod -a -G group1,group2,group3 exampleuser
add group to multiple users .............. $gpasswd -M user1,user2,user3 examplegroup (Note: This will remove unlisted users from the group!)
add group to multiple users .............. $for user in user1 user2 user3; do sudo usermod -a -G examplegroup "$user"; done
remove user's group2 (is in group1,2,3) .. $usermod -G group1,group3 exampleuser
force a new primary group for user ....... $usermod -g examplegroup exampleuser

X server (desktop / graphical ui)

gnome desktop
close the X server ....................... $sudo /etc/init.d/gdm stop
start the X server ....................... $sudo /etc/init.d/gdm start
restart the X server ..................... $sudo /etc/init.d/gdm restart
kde desktop
close the X server ....................... $sudo /etc/init.d/kdm stop

Date and time

view the current date and time ........... $date
set date time to 28.6.2010 13:45:55 ...... $date 062813452010.55
change current time zone (recommended) ... $dpkg-reconfigure tzdata
change current time zone (deprecated) .... $tzconfig
change current time zone ................. $tzselect
sets the current time/date for file ...... $settime myfile.txt
create or change accessed time if exists . $touch newfile.txt

Sync your clock with NTP servers (Network Time Protocol)

http://codeghar.wordpress.com/2007/12/06/manage-time-in-ubuntu-through-command-line/

Job managing

Job and task handling tips

show ongoing jobs/tasks/programs ......... $jobs
cancel/terminate job ..................... ctrl+c
stop/pause job ........................... ctrl+z
bring the most recently backgrounded or
stopped job to the foreground ............ $fg
move stopped job to bg based on job id ... $bg 3
run as a background task (use '&') ....... $drush search-index &

Note! If a task was started and is running in the foreground, it is still possible to move it to the background without cancelling it:

  1. ctrl-z, this will suspend the current foreground job (task).
  2. Enter the job control command 'bg'
  3. Tap the 'Enter' key

The job is now running in the background

Administration related commands

Operating system restart / reboot / shut down

restart your system ...................... $shutdown -r now
bring down the system immediately ........ $shutdown now
bring down and power off the system ...... $shutdown -P now

crontab

edit current user's crontab .............. $crontab -e
edit theuser user's crontab .............. $crontab -u theuser -e
list current user's crontab .............. $crontab -l

Process handling

Show processes

display all running processes ............ $ps -A (ps stands for "process status")
display all running processes ............ $ps -e
display all running processes ............ $ps aux | less  (-a all w/ tty except session leaders, -u by effective user ID (supports names), x processes w/o controlling ttys)
display all 'apache' processes ........... $ps aux | grep apache
display ps command help .................. $ps --help
display all 'firefox' process ids ........ $pgrep firefox
display all 'firefox' process ids ........ $pidof firefox
show exampleuser's all processes ......... $ps -u exampleuser
show running processes as a tree ......... $pstree
live-status of current top CPU processes . $top (q = quit, h = help)

Kill processes

terminate process with id 3486 ........... $kill 3486
kill process 3486 (with "force") ......... $kill -9 3486
kill all firefox processes (not unix!) ... $killall -9 firefox (DO NOT USE killall command on UNIX system as it's Linux only command)

Server related commands

show process info, e.g. cpu-time & mem ... $top (q = quit, h = help)
restart apache web server ................ $/etc/init.d/httpd restart

Network related commands

Network interface commands

show network interfaces .................. $ifconfig
show eth0 interface ...................... $ifconfig eth0
shut down eth0 interface, releases ip .... $ifconfig eth0 down
enables the eth0 interface, renews ip .... $ifconfig eth0 up
restart the network using the values
in /etc/network/interfaces ............... $/etc/init.d/networking restart

SSH

open ssh connection ...................... $ssh exampleuser@iporhost.name
open ssh connection with debug info ...... $ssh -v exampleuser@iporhost.name
open ssh connection using custom port .... $ssh exampleuser@123.3.2.1 -p123
force ssh to use specific private key .... $ssh -i ~/.ssh/id_rsa_foo user@exampleserver.com
specify private key, port and user ....... $ssh -i /vm/rackspaceclone/puphpet/files/dot/ssh/root_id_rsa -p 2222 root@127.0.0.1
close (the most recent) connection ....... $exit

If you get an error message during idle session the service have probably timeouted: Write Failed: broken pipe. Edit (or create) /etc/ssh/ssh_config for Linux and ~/.ssh/config for mac and add the first line. If that don't help you can try the others as well.

ServerAliveInterval 120
ClientAliveInterval 120
TCPKeepAlive yes

screen

SSH sessions may break due to numerous and possibly unavoidable reasons.

A useful utility which can be used to mitigate problems caused by this is called screen. Screen is a powerful utility that allows you to control multiple terminals which will stay alive independently of the ssh session. For example, if you run screen in an ssh session you will see a new terminal open and you can use that to run jobs. Lets say your ssh session dies in the process. Running screen -d then screen -r will reopen the last session and you will be able to continue from there.

Start a screen session ...................... $screen
Start a screen session ...................... $ctrl-a c
End a screen session ........................ $exit
End a specific screen session, e.g. 1234 .... $screen -X -S 1234 quit (sends a quit command for specified session)
Get the list of current screen sessions ..... $screen -list / screen -ls
check opened screens and process tree ....... $ps auxfw
If you have multiple screens open go next ... $ctrl-a space / ctrl-a n / ctrl-a ctrl-n
Go to the previous active screen ............ $ctrl-a p / ctrl-a ctrl-p
Detach/disconnect screen from your terminal . $screen -d
Reattach a session .......................... $screen -r
Open screen with a specific title ........... $screen -t thescreenname

If you get following message: -bash: screen: command not found, install screen first.

yum install screen
or
apt-get install screen

If you get following message:

There are several suitable screens on:
	64917.ttys002.Johns-MacBook	(Detached)
	64960.ttys001.Johns-MacBook	(Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

Reattach a specific session (pid) ........... $screen -r 64917

SFTP

open sftp connection ..................... $sftp exampleuser@123.123.123.123
show sftp commands once connected ........ sftp> help
download file to local computer .......... sftp> get /home/user/file1.ext
list folders and files on sftp server .... sftp> ls
list folders and files on local computer . sftp> lls

See commands also from: http://linux.about.com/od/commands/l/blcmdl1_sftp.htm

SCP

  • scp stands for secure cp (copy), which means that you can copy files across an ssh connection that will be encrypted, and therefore secured.

Copy the file "foobar.txt" from a remote host to the local host dir

$ scp exampleuser@iporhostname.com:foobar.txt /some/local/directory

Copy the file "foobar.txt" from a remote host to the current dir in local host using port 22

$ scp -P 22 exampleuser@iporhostname.com:foobar.txt .

Copy the file "foobar.txt" from the local host to a remote host

$ scp foobar.txt exampleuser@iporhostname.com:/some/remote/directory 

Copy the directory "foo" from the local host to a remote host's directory "bar"

$ scp -r foo exampleuser@iporhostname.com:/some/remote/directory/bar 

Copy all the files and sub directories from the remote host's directory "foo" to a local host's current directory

$ scp -r exampleuser@iporhostname.com:/some/remote/directory/foo/* .

Copy all the files and sub directories from the remote host's directory "foo" starting with "a" letter to a local host's parent directory's subdirectory "bar"

$ scp -r exampleuser@iporhostname.com:/some/remote/directory/foo/a* ../bar/

Copy the file "foobar.txt" from remote host "host1.com" to remote host "host2.com"

$ scp username@host1.com:/some/remote/directory/foobar.txt \
username@host2.com:/some/remote/directory/ 

Copying the files "foo.txt" and "bar.txt" from the local host to your home directory on the remote host

$ scp foo.txt bar.txt exampleuser@iporhostname.com:~ 

Copy the files "foo.txt" and "bar.txt" from the remote host to current local directory

$ scp exampleuser@iporhostname.com:"/dir1/foo.txt /dir2/bar.txt" .

Copy the files "foo.txt", "bar.txt" & "baz.txt" from the remote host to current local directory

$ scp exampleuser@iporhostname.com:/dir\{foo.txt,bar.txt,baz.txt\} .

Copy the file "foobar.txt" from the local host to a remote host using port 2264

$ scp -P 2264 foobar.txt exampleuser@iporhostname.com:/some/remote/directory

Copy file from remote server using port 341 to local users home folder's /Sites/bu subdirectory

$ scp -P 341 exampleuser@xxx.xxx.xxx.xxx:/home/exampleuser/drupal6_bu_20130130.sql.gz ~/Sites/bu

Different commands

download a file using wget ............... $wget http://ftp.drupal.org/files/projects/panels-6.x-3.9.tar.gz
download multiple files at the same time . $wget http://foo.org/file1.txt http://foo.org/file2.txt http://foo.org/file3.txt

Linux administration

Installing packages

Apt-get

Updating system