Linux Commands

  • View contents of a binary file:

xxd –b FirstProgram.class

  • 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

  • To count the number of .java files in a directory recursively:

find ./path/to/dir/ -mindepth 1 -type f -name "*.java" | wc -l

  • To count all files in the current directory whose names start with _

ls ./_* | 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/

  • Copying large number of files, when mv says "argument list too long" (no * or anything else added after /)

rsync -a {source}/ {destination}/

  • To find out what OS you're running

cat /etc/

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

  • To find out hardware specs:

Comprehensive hardware info:

sudo lshw


sudo fdisk -l


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 find all pdf files and remove them (when regular rm says "too many arguments"):

find . -name "*.pdf" -print0 | xargs -0 rm

    • 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


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

if it's a .tar file, use tar xvf fileName.tar

    • To extract file filename.tar.gz:

tar xvzf filename.tar.gz

    • To zip a folder:

zip -r folder

    • To unzip:


    • In order to copy files from this server to remote server to directory /home/akashliev/ (say, remote server has ip address

scp index.html akashliev@

    • To copy directory build run:

scp -r build/ 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):


    • 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 list all files/directories in the current folder with their sizes:

du -sh *

  • To find the folder/file size:

du -sh dirName


du -sh fileName

  • 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

  • IndexOf in bash:

$ a="some long string"

$ b="ri"

$ echo ${a/*$b/$b}


$ echo ${a/$b*/$b}

some long stri

  • To open a text file and "watch" it, i.e. make it auto-refresh whenever its contents are changed:

tail -f fileName.txt

  • to compile java project from command line (into a directory "classes" which one should create before running the below command):

javac -cp "./lib/*" src/*.java src/*/*.java src/*/*/*.java -d classes

cd classes

java -cp ".:../lib/*" test.T2020_02_24_genGeoSci2 20

  • to zip and password-protect the archive in a mac:

zip -er folderToBeZipped