Post on 19-Mar-2023
Introduction to Basic Linux
Delilah MaloneyHigh Performance Computing (HPC) Specialist
Research TechnologyTufts Technology Services
Delilah.Maloney@tufts.edutts-research@tufts.edu
Linux
• Operation System, just like Unix, Windows or OS X.• Linux was created in 1991 by Linus Torvalds as a free and open
source alternative to Minix, another Unix clone that was predominantly used in academic settings.
Linux
• Hobby
Ø 75% stock exchange
Ø Web servers
Ø Electronic devices
Ø 95% Supercomputers and ALL of the TOP500s
Type Alone
q If you have access to Tufts HPC cluster:• Terminal (from Linux or Mac)• ssh utln@login.cluster.tufts.edu
• OnDemand Web Interface (from Windows)• https://ondemand.cluster.tufts.edu• Clusters à Tufts HPC Shell Access
q If you haven’t get your access to Tufts HPC cluster:• CentOS terminal online by Coding Group : • https://www.tutorialspoint.com/unix_terminal_online.ph
p • Some of the commands may not be available
Commands
q Basic structure of Linux commands:§ command§ command options § command options arguments
q Two versions of options: Short (-a) and/or Long (--all)q Most of the options can Mix and Match!q The wildcard “*” (asterisk or star)q Execute “./”q Run commands from a file “source” or “.”q Case Sensitive!
§ “File” and “file” are different files!
Tips & Tricks
q Tabq Control + A & Control + Eq Control + Cq The arrow keys are your friend (Up & Down)q Put your most used set of commands in a script
System
How to get user information:• whoami
Ø Display username• groups
Ø Display the groups you belong to
System
How to get user information:• whoami
Ø Display username• groups
Ø Display the groups you belong to• id
Ø Display your username and groups
System
How to get user information:• whoami
Ø Display username• groups
Ø Display the groups you belong to• id
Ø Display your username and groups• getent group name_of_the_group
Ø Display the members’ usernames in a group
System
To get information about your system:Memory information:• free –g or –m or -h
Ø Display host memory (RAM) spec in GB/MB/human readable
System
To get information about your system:Memory information:• free –g or –m or -h
Ø Display host memory (RAM) spec in GB/MB/human readable
• quotaØ Display storge/memory quota
System
To get information about your system:Memory information:• free –g or –m or -h
Ø Display host memory (RAM) spec in GB/MB/human readable
• quotaØ Display storge/memory quota
• showquotaØ Unique to Tufts HPC clusterØ Display home directory quota and project space quota
System
To get information about your system:Memory information:• free –g or –m or -h
Ø Display host memory (RAM) spec in GB/MB/human readable
• quotaØ Display storge/memory quota
• showquotaØ Unique to Tufts HPC clusterØ Display home directory quota and project space quota
• du -shØ Display disc usage summary in human readable form
System
To get information about your system:System information:• hostname < –i >
Ø Display system hostnameØ Display IP address of the host
System
To get information about your system:System information:• hostname < –i >
Ø Display system hostnameØ Display IP address of the host
• lscpu <–e=[list]>Ø Display CPU information (compact)Ø Display selected CPU information
System
To get information about your system:System information:• hostname < –i >
Ø Display system hostnameØ Display IP address of the host
• lscpu <–e=[list]>Ø Display CPU information (compact)Ø Display selected CPU information
• cat /proc/cpuinfoØ Display CPU information (Individually)
System
To get information about your system:System information:• hostname < –i >
Ø Display system hostnameØ Display IP address of the host
• lscpu <–e=[list]>Ø Display CPU information (compact)Ø Display selected CPU information
• cat /proc/cpuinfoØ Display CPU information (Individually)
• nproc < --all >Ø Number of available processors
Help!
Most commands have a manual, if not, most of them have help:• man command
Ø Display command manual• man –k keyword
Ø Search all manuals based on keyword• command –h or --help
Ø Help info
...And Google helps!
System
Try it out yourself!• whoami• group• Id• free –h• quota• showquota (on Tufts HPC Cluster)• hostname• lscpu• cat /proc/cpuinfo• nproc• man <any command>• <command> --help
Directory & File
Navigate in the system, editing files, working with directories, .etc. • mkdir dir_name
Ø Create a directory
Directory & File
Navigate in the system, editing files, working with directories, .etc. • mkdir dir_name
Ø Create a directory • rm file_name
Ø Remove a file• rm –r or -rf dir_name
Ø Remove a directory with it’s contents• rmdir
Ø Remove an empty directory
Directory & File
Navigate in the system, actions in directory level:• pwd
Ø Show current directory path
Directory & File
Navigate in the system, actions in directory level:• pwd
Ø Show current directory path• cd directory_path
Ø Go the “directory_path” location• cd ~ or cd
Ø Go to home directory• cd ..
Ø Go up a level of the directory• cd –
Ø Go back to the last directoryyou were in
Directory & File
Listing directory contents:• ls
Ø Display contents in the current directory• ls –a
Ø Display ALL contents in the current directory• ls –l
Ø Display long listing of the contents• ls –d */
Ø Display directory entries only• ls –r or –t or –X or –R or .etc
Ø Display in reverse order/sort by time/sort by extension/recursively
Directory & File
Text editors:• nano• vi• vim• emacs• atom• geany• brackets• Kwrite• sublime• gedit• OnDemand!
Directory & File
Try it out yourself!• cd ~• ls• mkdir test1 test2• ls• rmdir test2• ls• cd test1• pwd• nano file1.txt• Hello World
• ls
Directory & File
Navigate in the system, editing files, working with directories, .etc. The object that you work on:• cp file1 file2 or dir
Ø Copy file1 to file2• cp –r dir1 dir2
Ø Copy dir1 to dir2 with contents
Directory & File
Navigate in the system, editing files, working with directories, .etc. The object that you work on:• cp file1 file2 or dir
Ø Copy file1 to file2• cp –r dir1 dir2
Ø Copy dir1 to dir2 with contents• mv file1 file2
Ø Rename file1 as file2
Directory & File
Navigate in the system, editing files, working with directories, .etc. The object that you work on:• cp file1 file2
Ø Copy file1 to file2• cp –r dir1 dir2
Ø Copy dir1 to dir2 with contents• mv file1 file2
Ø Rename file1 as file2• ln –s dir_path new_dir_path
Ø Create a symbolic link to dir_path
Directory & File
Navigate in the system, editing files, working with directories, .etc. The object that you work on:• cp file1 file2
Ø Copy file1 to file2• cp –r dir1 dir2
Ø Copy dir1 to dir2 with contents• mv file1 file2
Ø Rename file1 as file2• ln –s dir_path new_dir_path
Ø Create a symbolic link to dir_path• nl –b a file1
Ø Number lines in file1,including the empty lines.
Directory & File
• clearØ Clear the terminal screen
• Command 1 | command 2Ø Use output of command 1 as the input of command 2
Directory & File
• clearØ Clear the terminal screen
• Command 1 | command 2Ø Use output of command 1 as the input of command 2
• Command > fileØ Save output to file
• Command >> fileØ Append output to file
• Command 2> fileØ Redirect error message to file
• Command < fileØ Use data from file as input of command
Directory & File
Work with text files:• cat file_name
Ø Display file content• less file_name
Ø Display a long text file per page at a time• more file_name
Ø Display a long text file per page at a time
Directory & File
Work with text files:• cat file_name
Ø Display file content• less file_name
Ø Display a long text file per page at a time• more file_name
Ø Display a long text file per page at a time• head file_name
Ø Display first 10 lines of a file• tail file_name
Ø Display last 10 lines of a file
Directory & File
Try it out yourself!• cp file1.txt file2.txt• mv file2.txt newfile2.txt• ln –s link dir• seq 1 2 100 > file3.txt• less file3.txt• Seq 2 2 100 >>file3.txt• head file3.txt• tail file3.txt• Typenonsense 2> error.err• cat error.err• Seq 2 3 100 | tail
Directory & File
Work with text files:• paste <-s> file1 file2 > file3
Ø Combine lines in file1 and file2, save output to file3
Directory & File
Work with text files:• paste <-s> file1 file2 > file3
Ø Combine lines in file1 and file2, save output to file3• csplit file line_number <–f prefix>
Ø Split file at the end of line_number and name the output files as prefix00 prefix01
Directory & File
Work with text files:• paste <-s> file1 file2 > file3
Ø Combine lines in file1 and file2, save output to file3• csplit file line_number <–f prefix>
Ø Split file at the end of line_number and name the output files as prefix00 prefix01
• fold –w width –s file1Ø Wrap each line to fit in a specific width (default 80), break
at space
Directory & File
Work with text files:• paste <-s> file1 file2 > file3
Ø Combine lines in file1 and file2, save output to file3• csplit file line_number <–f prefix>
Ø Split file at the end of line_number and name the output files as prefix00 prefix01
• fold –w width –s file1Ø Wrap each line to fit in a specific width (default 80), break
at space• diff file1_name file2_name• sdiff file1_name file2_name
Ø Difference between two filesØ Side by side comparison
Directory & File
Searching for something? Look no further:• grep pattern file
Ø Search text for specific pattern• grep –i pattern file
Ø Search with case insensitive pattern• grep --color pattern file
Ø High pattern in search result• grep –r pattern dir
Ø Search pattern recursively. • grep –c pattern file
Ø Count the matching case
Directory & File
Searching for something? Look no further:• find dir_name –name file_name
Ø Find file “file_name” in directory dir_name• find dir_name –atime or -mmin n
Ø The file was last accessed more than n days or minutes ago• find dir_name –iname file_name
Ø Search for file_name ignore case• find dir_name –executable
Ø Search for executable files• which
Ø List the path for a commad
Directory & File
Navigate in the system, editing files, working with directories, .etc. The object that you work on:• wget file_url
Ø Download file from web
Directory & File
Navigate in the system, editing files, working with directories, .etc. The object that you work on:• wget file_url
Ø Download file from web• tar –cf or -xf file_list or file.tar
Ø Compress or Extract files • gzip (-l) file_name
Ø Compress file to .gz (with info)• gzip –d or gunzip file.gz
Ø Decompress file.gz• unzip file.zip
Ø Decompress file.zip
Directory & File
Changing permissions!• chmod permissions file_name
Ø permissions:o u: user(owner) g: group o: otherso r: read (4) w: write (2) x: execute (1) o No permission (0)
Ø chmod u=rwx,g=rx,o=r file_nameØ chmod 754 file_name
o User have the right to read, write, and executeo Group members have the right to read and executeo Others have the right to read
Ø chmod +x file_nameo Executable for owner and group
Directory & File
Changing owner and group!• chown ownername file/dir
Ø Change the owner of a file or directoryØ Use –R for recursively change ownership under a directory
• chown :groupname file/dir• chgrp groupname file/dir
Ø Change the group of a file or directoryØ Use –R for recursively change group under a directory
Tips & Tricks
q Change the permission of your scriptØ chmod +x script.sh
q Create aliases for common commandsØ alias mybook=“echo cat in the hat”Ø alias interact=“srun -p interactive –n 1 --pty --x11=first --
mem=8G bash”
Directory & File
Try it out yourself!• paste –s file1.txt file3.txt > file13.txt• fold file13.txt –w 60• grep --color 9 file3.txt• which wget• chmod +x file3.txt
More Information:
Wiki:• go.tufts.edu/cluster OR • https://wikis.uit.tufts.edu/confluence/display/TuftsUITResear
chComputing/Homego to High Performance Compute Cluster/Linux
Linux Reference:• https://linoxide.com/guide/linux-command-shelf.html• https://www.howtoforge.com/linux-commands/
Google it!