Version 3 (modified by 10 years ago) ( diff ) | ,
---|
Linux Commands
Basic Linux Commands
Here are a few of the common Linux commands you will use to navigate the clusters and manipulate files and directories.
man
Display online manual pages. Most Linux commands have a manual page with detailed instructions on use. Replace <command name> below with command you would like information on.
[tulaneID@cypress1 ~]$ man <command name>
pwd and cd
The pwd command lists the present working directory. The cd command changes directory.
[tulaneID@cypress1 ~]$ pwd /home/tulaneID [tulaneID@cypress1 ~]$ cd NextDirectoryUp [tulaneID@cypress1 ~]$ pwd /home/tulaneID/NextDirectoryUp
ls
List files.
[tulaneID@cypress1 ~]$ ls a.out code.c Makefile
Setting the -l flag will display files along with their permissions and ownership.
[tulaneID@cypress1 ~]$ ls -l total 0 -rw-r--r-- 1 tulaneID MyGroup 0 Aug 14 10:57 a.out -rw-r--r-- 1 tulaneID MyGroup 0 Aug 14 10:57 code.c -rw-r--r-- 1 tulaneID MyGroup 0 Aug 14 10:57 Makefile
mkdir
Create new directory.
[tulaneID@cypress1 ~]$ cd testdir -bash: cd: testdir: No such file or directory [tulaneID@cypress1 ~]$ mkdir testdir [tulaneID@cypress1 ~]$ cd testdir [tulaneID@cypress1 ~]$ pwd /u00/user/testdir
rm
Remove files and directories. CAUTION: THERE IS NO UNDOING THIS COMMAND.
[tulaneID@cypress1 ~]$ rm testfile
If you would like to remove a directory and all of its contents use the following command:
[tulaneID@cypress1 ~]$ rm -ri testdir rm: descend into directory `testdir'? y rm: remove regular empty file `testdir/Makefile'? y rm: remove regular empty file `testdir/code.c'? y rm: remove regular empty file `testdir/a.out'? y rm: remove directory `testdir'? y [tulaneID@cypress1 ~]$
The r flag specifies to remove files/directories recursively. i specifies to prompt before deleting each file.
If you are confident with the command line and your understanding of file locations you can use the f flag instead of i to force deletion of all files without prompting: {{ [tulaneID@cypress1 ~]$ rm -rf testdir [tulaneID@cypress1 ~]$ ls testdir ls: cannot access testdir: No such file or directory }}
cp
Copy file to new location.
In the example below file1 already exists, and file2 will become the copy.
[tulaneID@cypress1 ~]$ cp file1 file2 To copy a directory and all of its contents:
[tulaneID@cypress1 ~]$ cp -r dir1 dir2 mv Move a file to a new location.
[tulaneID@cypress1 ~]$ mv file1 file2 [tulaneID@cypress1 ~]$ ls file1 ls: cannot access file1: No such file or directory [tulaneID@cypress1 ~]$ ls file2 file2 Unlike cp, the mv command does not create a second instance of the file or directory.
quota Display disk quotas.
The -s flag translates the output into a readable format. If your blocks column is equal to or greater than the quota column, you have exceeded your available disk space.
user@sphynx>quota -s Disk quotas for user wcurry (uid 65098): Filesystem blocks quota limit grace files quota limit grace /scratch03 86978M 1024G 2048G 180k 0 391m /u01 2723M 40000M 40050M 19136 0 0 cat Print the entire contents of a file.
[tulaneID@cypress1 ~]$ cat daysofweek.txt monday tuesday wednesday thursday friday saturday sunday more more is like cat, except it prints the file one page at a time. The spacebar is used to continue on to the next page.
grep Find text in a file. grep will return every line in the file that matches your search term.
[tulaneID@cypress1 ~]$ cat animals dog Dog cat Racoon DOG bullfrog Little Doggie
[tulaneID@cypress1 ~]$ grep dog animals dog Use the -v flag to print out every line excluding those containing the search term. Notice that "dog" (lowercase) is missing from the results.
[tulaneID@cypress1 ~]$ grep -v dog animals Dog cat Racoon DOG bullfrog Little Doggie The -i flag will search without case-sensitivity.
[tulaneID@cypress1 ~]$ grep -i dog animals dog Dog DOG Little Doggie The -i and -v flag can be combined to exclude all lines containing the search term regardless of capitalization.
[tulaneID@cypress1 ~]$ grep -iv dog animals cat Racoon bullfrog sed sed is a powerful text stream editor. There are many uses of this program, but the most common is its search and replace function. Combined with regular expressions, this command can be used to seek out complex strings in your code and replace them with modifications. Please visit Sed - An Introduction and Tutorial for more information.
[tulaneID@cypress1 ~]$ cat animals dog Dog cat Racoon DOG bullfrog Little Doggie
[tulaneID@cypress1 ~]$ sed 's/dog/bird/g' animals bird Dog cat Racoon DOG bullfrog Little Doggie awk awk is a programming language used to modify files. One common use of awk is printing specific columns of a text stream or file.
[tulaneID@cypress1 ~]$ cat sample.data 0.3 0.22 1.8 3.1 2.34 3.0 0.2 1.0 3.2
[tulaneID@cypress1 ~]$ awk '{print $1 " " $3}' 0.3 1.8 3.1 3.0 0.2 3.2 The above command printed out the 1st and 3rd columns of the sample.data file.
For more information on awk please visit Awk - A Tutorial and Introduction.
tail Displays lines from the end of a file. Useful for viewing recent results in an output file.
[tulaneID@cypress1 ~]$ cat animals dog Dog cat Racoon DOG bullfrog Little Doggie
[tulaneID@cypress1 ~]$ tail -3 animals DOG bullfrog Little Doggie The -f flag can be used to tail a file interactively. New additions to the end of the file will be printed to your screen.
head Head is much like tail, except it prints from the top of a file.