Greetings,
This isn't majorly a Mechanics post but I've noticed that a lot of people have started using HPC's (which use Linux OS almost all the time), and Linux OS based desktops. It's slightly difficult for people to begin using linux but once you do there's no turning back. Here I aim to create a Linux Command cheat sheet that one can refer to. Or just have it printed and kept in the lab.
Most of these were borrowed from tecmint.com but I've added in some more explanations and commands.
----------------------------------------------------------------------------
$ Instructions on how to enter commands
$ command is case sensitive, the flags are used after command. For ex:
$ ls -l = here ls is the command and -l is the flag
$ multiple flags can be written in minute form by putting them together
$ ls -l -a -R can be written as ls -laR
$ Press up to see command history
$ Press tab to autofill commands
$ Use wildcards to work with specific items:
$ example: ls *.jpg will list only jpg items
$ ls test.* will show only filenames with *
$ ls **or** will list files with or in their filenames
$ some of these commands might only work on redhat and not on other distros
$ like ubuntu, for example the yum search and yum install commands
$ are redhat commands and for ubuntu have to be replaced with
$ apt-get
----------------------------------------------------------------------------
1.uptime Command
In Linux uptime command shows how long your system has been running and the number of users currently logged in and also displays load average for 1,5 and 15 minutes intervals.
2. w Command
It will displays users currently logged in and their running process. Also shows the login name, tty name, remote host, login time, idle time, JCPU, PCPU, command and processes.
-h : displays no header entries.
-s : without JCPU and PCPU.
-f : Removes from field.
-V : (upper letter) – Shows versions.
3. users Command
Users command displays currently logged in users.
4. who Command
who command simply returns user name, date, time and host information. who command is similar to w command. Unlike w command who doesn’t print what users are doing.
-b : Displays last system reboot date and time.
-r : Shows current runlet.
-a, –all : Displays all information in cumulatively.
5. whoami Command
whoami command prints the name of current user. You can also use “who am i” command to display the current user. If you are logged in as a root using sudo command.
6. ls Command
ls command displays list of files in human readable format.
-ltr
Sort file as per last modified time.
-l shows file or directory, size, modified date and time, file or folder name and owner of file and it’s permission.
ls -a
List all files including hidden file starting with ‘.‘.
-lh
shows sizes in human readable format.
-F
add the ‘/’ Character at the end each directory.
-r
display files and directories in reverse order.
-R
will list very long listing directory trees.
-lS
displays file size in order, will display big in size first.
--versions
check ls version (yes, its a double dash)
-l <direcctory name>
list files under directory
-ld <directory name>
displays information
-d */
display only directories
7. halt
If you are logged in as root, issuing the halt command will cease all CPU function on the system. On most systems, this will drop you into single-user mode and then power off the machine. (Probably a red hat only command)
8. less Command
allows quickly to view file. You can page up and down. Press ‘q‘ to quit from less window.
9. more Command
more command allows to quickly view file and shows details in percentage. You can page up and down. Press ‘q‘ to quit out from more window.
If some text quickly removes itself from the terminal window then you can use more as an addendum to force text output to be page by page controlled by the user like this:
ls -l |more
10. cp Command
Copy file from source to destination.
example: cp fileA fileB
example cp /tmp/fileA.jpg /usr/AnotherFolder/fileB.jpg
-i
You will be prompted before overwrite to file.
11. mv Command
Rename fileA to fileB or move a file to a different directory
example of rename: mv fileA fileArenamed
example of move: mv fileA /tmp/directory/subdirectory/
-i
You will be prompted before overwrite to file.
12. cat Command
cat command is used to view multiple file at the same time.
example: cat fileA fileB or cat fileA;cat fileB; cat FileC;
cat can show contents of files
example: cat /usr/directory1/file.txt
creates a file, Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d‘) to exit. The text will be written in file. You can see contains of file with following cat command.
example: cat >test
redirect text of a file(s) into a new file or else existing file with ‘>‘ (greater than) symbol. Careful, existing contains of test3 will be overwritten by contains of test, test1 and test2 file.
example: cat test test1 test2 > test3
Appends in existing file with ‘>>‘ (double greater than) symbol. Here, contains of test file will be appended at the end of test1 file.
example: cat test >> test1
-n
see the line numbers of all lines of a file
-b
see the line numbers of non empty lines only
13. Cd command (change directory)
example: cd /directory1/directory2/
14. pwd command (print working directory)
pwd command returns with present working directory. Great for bash scripting
15. Sort command
Sorting lines of text files in ascending order.
-r
descending order
16. vim Command
Vim is a popular text editor available on most of the UNIX-like OS.
example: vim test.txt
will create test.txt or open it if it exists.
Press :q to exit. Press :q! to force exit. Press :w to save. Press :wq to save and quit.
17. ssh Command (Secure Shell)
SSH command is used to login into remote host. Great for working remotely. Use ifconfig to find out inet address and then ssh into that computer using its inet address.
example: ssh universityid [at] HPCname.Servername.UniversityU.edu
ssh 11.111.111.111
-X
Switch on X11 forwarding
-Y
switch on trusted X11 Forwarding
ANY linux system (even your personal computer) can be remoted into if it has the open-ssh libraries installed. Install open ssh libraries in ubuntu with sudo get-apt open-ssh
All you need to know about the computer you want to remote into is the ipaddress which you can find using command 36: ifconfig
18. ftp or sftp Command
ftp or sftp command is used to connect to remote ftp host. ftp is (file transfer protocol) and sftp is (secure file transfer protocol).
Upload multiple files in remote host with mput
ftp > mput test1.txt
mget to download multiple files from remote host.
ftp > mget test2.txt
19. service Command
Service command call script located at /etc/init.d/ directory and execute the script. There are two ways to start the any service. For example we start the service called httpd with service command.
example: service <service name> start
20. free command
Free command shows free, total and swap memory information in bytes.
-t
shows total memory used and available to use in bytes.
21. top Command
top command displays processor activity of your system and also displays tasks managed by kernel in real-time. It’ll show processor and memory are being used. Press ‘O‘ (uppercase letter) to sort as per desired by you. Press ‘q‘ to quit from top screen.
-u <user_name>
shows username specific details
22. Tar Command
tar command is used to compress files and folders in Linux. For example the below command will create a archive for /home directory with file name as archive-name.tar.
tar -cvf archive-name.tar /home
To extract tar archive file use the option as follows.
tar -xvf archive-name.tar
-c
Create a new archive
-x
extract files from archive
-v
verbosely list files processed
-f
use archive file
-t
list the contents of an archive
-r
append files to end of archive
23. Grep Command
grep search for a given string in a file.
-i
ignore case sensitivity
24. find Command
Find command used to search files, strings and directories.
example: find /usr/ -name test
finds a file called test in directory and subdirectories of usr
-name <text>
search for files with name 'text'
-i
ignores case sensitivity
-type d -name
finds directories only
-type f -name
finds extensions
! -name <text>
Finds files without the text , 'text'
-type f -empty
finds empty files
-type f -name ".*"
finds all hidden files
/ -mtime 50
finds files modified in the last 50 days
/ -atime 50
finds files accessed in the last 50 days
/ -cmin -30
finds files changed in the last 30 mins
/ -mmin -30
finds files modified in the last 30 mins
/ -amin -30
find files accessed in last 30 mins
/ -size +50M -size -100M
finds files greater than 50 mb less than 100 mb
Combine these flags together to use modified options such as
find / -size +50M -size -100M -type f ! -name "*.mp3"
Will find any files greater than 50 mb less than 100 mb with an extension that is NOT mp3
25. lsof Command
List of all open files.
-u <username>
Opens list of files opened by particular user
26. last command
With last command we can watch user’s activity in the system. It will display complete user’s info like terminal, time, date, system reboot or boot and kernel version. Useful command to troubleshoot.
example: last or last <username>
27. ps command
ps command displays about processes running in the system.
example: ps -ef | grep init
28. kill command
Use kill command to terminate process. First find process id with ps command as shown below and kill process with kill -9 command.
example: ps -ef | grep init
root 1 0 0 07:53 ? 00:00:04 /sbin/init
root 7508 6825 0 11:48 pts/1 00:00:00 grep init
kill- 9 7508
29. rm command
rm command used to remove or delete a file without prompting for confirmation.
example: rm test.txt
-i
get confirmation before removing
-r
force delete
WARNING: Permanent deletion occurs of all files when rm is used
to delete contents of the entire directory use:
rm <directoryname>/*
30. mkdir command and rmdir command
mkdir command is used to create directories. rmdir command is used to delete directories
31. alias
make shortcuts to large commands that are used daily
example: alias trialcommand="find / -size +50M -size -100M -type f ! -name "*.mp3""
Now using the command trialcommand will find any files greater than 50 mb less than 100 mb with an extension that is NOT mp3
example: trialcommand
WARNING: aliases are forgotten everytime you logout
32.date
Shows date and time. Can be saved in a variable and used for timed bash scripting
33. chmod
Changes permissions from read only, to read and access only or read and write etc and vice versa. Very important command if you want to execute a file and getting a permission error
example: chmod <filename> number
numbers:
1st digit owner permission
2nd digit group permission
3rd digit others permission
7read, write and execute
6read and write
5read and execute
4read only
3write and execute
2write only
1execute only
0none
example chmod 764 test.for
test.for will have following permissions:
user can read, write and execute
group can read and write but not execute
others can only read but not write and execute
-R
recursive, i.e. include objects in subdirectories
-f
force, forge ahead with all objects even if errors occur
-v
verbose, show objects processed
34. man or help
The manual command or help command can be used to pull up help files for all commands
example: man ls
35. matlab -nodesktop
Opens matlab at the terminal, useful when you do not require the GUI and want to write quick codes or run matlab quickly
35. abaqus commands
abaqus licensing ru
shows licensing details of abaqus, will show how many licenses are being used
abaqus cae -mesa
starts abaqus CAE with mesa drivers, works better if you're on an older compuer
abaquse viewer odb=FILENAME
opens the abaqus viewer with the odb file opened
abaqus cae noGUI script=FILENAME.py
Run the python script
abaqus job=JOBNAME inp=INPUTFILENAME cpus=NO.OF.CPUS interactive
Run an interactive job
abaqus help |more
Shows the abaqus command help page
abaqus doc
opens the HTML Documentation
35. scp
secure copy can be used to copy files while remoting into a computer from the remote computer to user computer and vice versa
36. ifconfig
Outputs the computer's inet address which you can use to ssh into that computer.
37. kinit
Obtain and cache kerberos ticket granting ticket
38. aklog
obtain tokens for authentication to AFS
39. sudo su -
become root
40. yum search app_name
search for app in RHEL
41 sudo yum install app_name -y
install app
42. echo "3.4562+5.6543" |bc -l
does simple arithematic, use s(), c() for sin cos, a() for arctan, l() natural log,e() exp
example: echo '2*sqrt(25)+s(3.1415/2)' | bc -l
output: 10.999991282731238
****The below 3 commands are for Red Hat Linux
43. system-config-printer
configure printer
44. system-config-authentication
configure authentication
47. system-config-date
configure date and time setting
********
48. variable name=value; echo "$variable_name"
Will output value. This is how you can use variables in the bash shell
49. time <other_command>
Tells you the time it took to run the command other_command
example: time echo 'scale=5000;4*a(1)' |bc -l
this will calculate pi to 5000 digits and give you the time to calculate it
50. cat /proc/cpuinfo
Shows cpuinfo and computer info
51. umount /media/<USB Name>
Unmount USB drive
52. poweroff
Shutdown computer
53. reboot
Restart computer
_____________
Ashwin