Linux Commands

  • Basic vi commands:
  • To count the number of lines in a file:
  • wc -l <filename>
  • To count the number of files in a current directory:
  • ls | wc -l

    • To count the number of files in a directory recursively:
    find dirName -type f | wc -l
    • Removing large number of files. If the following error occurs when removing large list of files

    -bash: /bin/mv: Argument list too long


    then use this command (or its variation without sudo):

    sudo find . -name '*' | sudo xargs rm -r -f

    • Moving large number of files.
    find . -name "*.xml" -print0 | xargs -0 -I {} mv {} ../xml/


    • To find out what OS you're running

    cat /etc/issue.net

    note - does not work on SunOS (on SunOS I used "uname -a" command)

    • To find out hardware specs:
    Comprehensive hardware info:

    sudo lshw

    others:
    sudo fdisk -l
    blkid

    RAM and CPU info:

    cat /proc/meminfo

    cat /proc/cpuinfo

    Hard drive info:

    df -H

    RAM (user friendly, detailed slot-by-slot description)

    sudo dmidecode -t memory


    to find models of graphics and network cards (and other hardware):

    lspci -tv

    • Add user (-m option will ensure home directory is created)
    sudo useradd -m test

    adduser somename
    passwd somename

    • Delete user:

    deluser jondoe

    • Delete password:
    passwd -d username
    • To add new user and include him in a particular group, e.g. groupname:

    useradd username -g groupname

    • or to create user and create his home directory in /home:

    sudo useradd -d /home/bugzilla -m bugzilla

    • To change file (or folder) owner :
    sudo chown -R shiyong /home/shiyong/
    • To change file (or folder) owner and group:
    sudo chown -R shiyong:shiyong /home/shiyong/

    • To find out what groups user belongs to :

    groups username

    • To add user userid to groupd groupname:

    usermod -G groupname userid

    • find file with certain name - for example variables.none.tmpl, in certain directory tree - for example /var/www, (it will search through all the subdirectories as well) use:

    sudo find /var/www -name 'variables.none.tmpl'

    • To find all files that contain text ear: (this will look through all files in tmp/container/ directory and all its subdirectories)

    fgrep -r 'ear' tmp/container/*

    • To list all software packages installed on the current machine:
    rpm -qa | less
    • To find the full name of the package:
    rpm -qa | grep packageName
    e.g.:
    rpm -qa | grep mysql
    • To move all files in current directory to directory named destination:

    mv * home/andrey/destination/

    • To remove directory and all of its contents:
    rm -r -f dirname
    • To compress into .tar.gz use the following syntax:

    tar -zcvf archive_name.tar.gz directory_to_compress

    • To extract file filename.tar.gz:

    tar xvzf filename.tar.gz

    • To zip a folder:
    zip -r folder.zip folder
    • To unzip:
    unzip folder.zip
    • In order to copy files from this server to remote server to directory /home/akashliev/ (say, remote server has ip address 192.169.0.10):

    scp index.html akashliev@192.168.0.10:/home/akashliev/

    • To copy directory build run:

    scp -r build/ akashliev@192.168.0.10:/home/akashliev/

    • To list all partitions of the hard drive:

    sudo fdisk -l

    • To swapon partition (e.g. if partition name is /dev/sda3):

    sudo swapon /dev/sda3

    • To install .deb package in ubuntu

    sudo dpkg -i package.deb

    • To clear terminal  fully (without the possibility to scroll up after it as it is the case with 'clear' command):
    reset

    • To add user to sudoers group:
    sudo adduser <username> sudo
    • Create a symbolik link (linux analog of windows shortcuts):
    ln -s FullPathToTarget pathToDesiredSymbolicLink
    will create a symbolic link (shortcut). For example:

    ln -s /home/andrey/somedir/filename /home/andrey/

    will create a shortcut, named filename in my home directory (since I did not explicitly say what name I want) that will point to /home/andrey/somedir/filename file.
    Note: you need to specify full path (as opposed to relative) to the target file.\

    • To delete all files in current directory whose names begin with AnalyzeText:
    rm -- AnalyzeText*

    • To find the folder/file size:
    du dirName -h
    or
    du fileName -h
    • To run a command from local terminal on a remote linux server:
    sshpass ssh -o StrictHostKeyChecking=no username@ipAddress "mysql -u username -ppassword -e \"SELECT COUNT(*) FROM mydb.users\""

    • To download a website (including all files linked in it and subpages)
    wget -mk -np http://www.url.com
    Comments