Linux commands

Some Linux commands that I use and find practical to have online for my own purpose. If you need a Linux command library check out About.com.

#Update Linux search index if you are unsure if the index is up to date
updatedb

#Find processes as begin with "search" (search*)
ps auxxw|grep search
______________________________________________________________________
#Rename a file or folder by moving it
mv oldfile.txt newfilename.txt
______________________________________________________________________
#Execute commands as another user
sudo

#Restart Apache
sudo /etc/init.d/apache2 restart

sudo -s
//The -s (shell) option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in passwd.
______________________________________________________________________
#Run Sphinx indexer
/usr/local/bin/indexer --all --rotate
______________________________________________________________________
#Change ownership to a folder and its files recursive
chown owner:group -R /home/httpd/mysite/sites/all/themes/mytheme
______________________________________________________________________
#Change permission on a file
chmod 755 /home/nfs/mysite/www/sites/all/themes/mytheme/page-news.tpl.php
______________________________________________________________________
#move a folder tree
mv /home/orjan/mytheme/ /home/httpd/mysite/sites/all/themes/mytheme
______________________________________________________________________
#Remove the folder "mytheme" and all its content
rm -r /home/httpd/mysite/sites/all/themes/mytheme
-f = force
-r = recursive
-v verbose //Show what program do
______________________________________________________________________
#Edit your php settings
nano /etc/php5/apache2/php.ini
______________________________________________________________________
#Make backup from MySQL database
mysqldump --user=root --password="xyz" mydatabase > /home/myfolder/mybackupfile.sql
mysqldump --user=root --password=xyz mydatabase table1 table2 > /home/myfolder/mybackupfile.sql

gunzip -c /path/backup.Sql.Gz | mysql -h[database host - usually localhost] -u[username] -p[password] [database-name]


mysqldump -uUSERNAME -pPASSWORD -databases DATABASENAME1 DATABASENAME2 | gzip -9 > DATAFILE.sql.gz

______________________________________________________________________
#Log in to a mysql database
mysql -u username -p databasename
______________________________________________________________________
#Size of folder
du en mapps storlek
du -h (h= human readable)
______________________________________________________________________
#Create a folder
mkdir /home/myfolder/newfolder
______________________________________________________________________
#Some copying notes
cp -r  /mnt/nfs/mysite/files/logos/* /home/orjan/logos
cp -pr /mnt/nfs/mysite/www/sites/all/themes/* /var/www/mysite/sites/all/themes/
cp -pr /mnt/nfs/mysite/www/sites/all/modules/* /var/www/mysite/sites/all/modules/

#Copying folder "atck" to "themes". -pr = copy permission as well
cp -pr /mnt/nfs/mysite/www/sites/all/themes/atck /var/www/mysite/sites/all/themes/

#Copying from mounted server by remove, copy and list it
rm -r /var/www/mysite/sites/all/themes/atck
cp -pr /mnt/nfs/mysite/www/sites/all/themes/atck /var/www/mysite/sites/all/themes/
ls -l /var/www/mysite/sites/all/themes/atck
____________________________________________________________________
#View server processes
HTOP

#View tasks
TOP

____________________________________________________________________
#gzip - zip files

Compress a folder xyz recursive to a file called xyz_yyy.gz
-r recursive
-c standard out, meaning saving the original folder and make a compressed copy

gzip -cr xyz > xyz_yyy.gz

_____________________________________________________________________
#tar - compress a file
tar -cvf to_file.tar from_folder/.
tar -czf nytt.tgz nytt/.

_____________________________________________________________________
#fold and compress
tar -cvf to_file.tar from_folder/.
gzip to_file.tar

fold and compress in the same command
tar -zcvf the_new_file.tar.gz /path_to/folder_to_be_compressed

    z = compress the archive through gzip
    c = create a new archive
    v = verbose which means list the progress while creating the archive
    f = use archive file
____________________________________________________________________
#Unzip files
gunzip imagepicker-6.x-2.1.tar.gz //removes zip-file, leave a tar-file
tar -xvf imagepicker-6.x-2.1.tar  //Unpacks tar-file in a folder
rm imagepicker-6.x-2.1.tar        //Removes tar-file

watch -n 1 uptime\;mysqladmin --user=root --password=xyz--verbose processlist

_______________________________________________________________________
#Who is in the group
cd /etc
pico group
[groupname]:x:1001:user1, user2, user3
_______________________________________________________________________
#Search among my latest used commands
Ctrl R + start typing something you know is in the command your looking for and suggestions will show up. When you find the one just press enter and it will be executed again.

_______________________________________________________________________
#Cron via crontab
crontab -e edit
crontab -l list

min hour dayofmonth monthofyear dayofweek command
0-59/2 * * * * /usr/bin/wget -O - -q -t 1 http://my_site.com/cron.php  > /dev/null 2>&1

Append string >/dev/null 2>&1 to stop mail alert:
0 1 5 10 * /path/to/script.sh >/dev/null 2>&1
OR
0 1 5 10 * /path/to/script.sh &> /dev/null

Crontab syntax
field allowed values
----- --------------
minute 0-59

hour 0-23

day of month 1-31

month 1-12 (or names)

day of week 0-7 (0 or 7 is Sun, or use names)

____________________________________
# Restart cron
/etc/init.d/crond restart


____________________________________
# Read log file in runtime

tail -f /var/log/mysystem/log.txt
tail -n50 /var/log/mysystem/log.txt   - last 50 lines
_____________________________________
#backup
datevar=`date +%Y-%m-%d`
newfile=backup-$datevar.tar

echo "# Doing mysql backup...."
[ -f /backup/new/backup.sql ] && rm /backup/new/backup.sql || echo ""
mysqldump -u root -pXYZ --all-databases > /backup/new/backup.sql

echo "# Doing /hotel filesystem backup..."
[ -f /backup/new/hotel.tar ] && rm /backup/new/hotel.tar || echo ""
tar cf /backup/new/hotel.tar /hotel/.

echo "# Compressing files into one dated file..."
[ -f /backup/new.tar ] && rm /backup/new.tar || echo ""
tar cf /backup/new.tar /backup/new/*
mv /backup/new.tar /backup/$newfile

_______________________________________
Upload file to another server
scp /home/httpd/xyz/my_mod/my_mod.module  orjan@127.1.1.1:/home/orjan/upload

_______________________________________
Keep date updated
apt-get install ntpdate

add to crontab:
* */2 * * * /usr/sbin/ntpdate -s europe.pool.ntp.org > /dev/null

_______________________________________
Track processes
install:
apt-get install strace
run:
strace -p<PID>

in HTOP you just navigate with up/down arrows and press s

_______________________________________
Change timezone on Debian
/usr/bin/tzselect
and select from suggestions

_______________________________________
ln -s /hotel/global/.htaccess .htaccess

install php extention apt-get install php5-imap

sudo apt-get install htop

_______________________________________
kill multiple processes

first try filtering by:
ps -ef | grep a_string_i_want_to_kill
or
ps -ef | grep phpmyAdmin

then try filtering out the process id:s:
ps -ef | grep a_string_i_want_to_kill | awk '{print $2}'
or
ps -ef | grep phpmyAdmin | awk '{print $2}'

then execude the kill command:
kill -9 `ps -ef | grep a_string_i_want_to_kill| awk '{print $2}'`
or
kill -9 `ps -ef | grep phpmyAdmin | awk '{print $2}'`

sudo
Knowledge keywords: