Linux Commands
From HPC
(→Files and Directories) |
|||
Line 81: | Line 81: | ||
rmdir myworkdir | rmdir myworkdir | ||
+ | ==Getting help/documentation about a command== | ||
+ | |||
+ | ===Manual command=== | ||
+ | |||
+ | Most (if not all) commands will have an associated manual document | ||
+ | |||
+ | man <command> | ||
+ | man ls (get manual page on ls command) | ||
+ | man qsub (get manual page on qsub command) | ||
+ | |||
+ | ===Quick help/showing command options=== | ||
+ | |||
+ | Most commands you can run will have a command option that will show you a list of available options and summary help. This is often the command switch --help | ||
+ | |||
+ | <command> --help | ||
+ | ls --help | ||
+ | qsub --help | ||
+ | rm --help | ||
+ | |||
+ | ==File commands (viewing/editing/creating)== | ||
+ | These are tools that allow you to create/modify text files via the command line or in scripts. | ||
+ | |||
+ | ===Touch=== | ||
+ | |||
+ | Creates empty files ready for writing to | ||
+ | |||
+ | touch myfile.txt | ||
+ | |||
+ | ===Cat=== | ||
+ | |||
+ | Reads files and dumps them to the console, you can redirect to a file via > | ||
+ | |||
+ | //read single file | ||
+ | cat myfile | ||
+ | |||
+ | //read multiple files (myfiles1, myfiles2, myfiles3//0 | ||
+ | cat myfiles* | ||
+ | |||
+ | //read multiple files and dump in a file | ||
+ | cat myfiles* > combined.txt | ||
+ | |||
+ | //append one file to another | ||
+ | cat file1 >> file2 | ||
+ | |||
+ | ===Grep=== | ||
+ | |||
+ | Provides serach/filter operations on input (case sensitive) use regular expression. See "man grep" for more details | ||
+ | |||
+ | //read file, search for lines containing the word debug | ||
+ | cat mylog | grep debug | ||
+ | |||
+ | //read file, search for "results returned" and output to new file | ||
+ | cat mywork.txt | grep results\ returned > myresults.txt | ||
+ | |||
+ | ===Tail=== | ||
+ | |||
+ | To view the tail/bottom of a file. Very usefull to see the end of a log file. | ||
+ | |||
+ | //show the bottom lines of a file | ||
+ | tail mywork.log | ||
+ | |||
+ | //show the bottom 150 lines of a file | ||
+ | tail -n 150 myfile | ||
+ | |||
+ | //Show the bottom of a file and stays runing to show you new additional lines to the file. This will hold the terminal (i.e. it waits till you press crtl+C before you can run another command) | ||
+ | tail -f mywork.log | ||
+ | |||
+ | ===Pipe output of one program into another=== | ||
+ | |||
+ | Using a pipe character ( | ) you can redirect the console output of one file into another. | ||
+ | |||
+ | command1 | command2 | ||
+ | |||
+ | ===Redirect output to a file (new/append)=== | ||
+ | |||
+ | Using the characters > < >> << you can redirect console out put to files. | ||
+ | |||
+ | //Redirect the output of the ls command to a new file (it will over write and exsiting file of the same name. | ||
+ | ls > mydirectorylisting.txt | ||
+ | |||
+ | //Redirect the output of a cat command and append it to an existing command | ||
+ | cat file2 >> file1 | ||
[[Category:Getting Started]] | [[Category:Getting Started]] |
Revision as of 11:21, 23 October 2008
Contents |
Linux Commands
Remember Linux is Case Sensitive. myscript is different to MyScript.
Files and Directories
Copy files
cp from to
Examples
cp * mywork/
Copies all files in current directory to a sub directory called mywork.
cp myfile.txt myfile.txt.backup
Copies a file in the same directory and renames that file.
Copy directories
cp -R directory destinantion
The -R switch means recursive, meaning it will copy the contents of subdirectories as well.
Examples
cp -R dir1 dir2
This copy dir1 and it's contents to dir2. The end result is dir2/dir1/files
cp -R dir1/ dir2/
This copies the contents of dir1 (but not actually the direcory it's self) to dir2. Note dir2 must already exist. The end result is dir2/files
Moving Files/Directories
Move file to a different directory
mv myfile dir/to/move/to/
Move files using wild cards
mv file* dir/ (would move any file starting "file" i.e. file1, file2, file-that-should-be-moved)
Move the contents of a directory (files + sub directories) to another directory.
mv mydir/* mydir2/
As above but move to current directory
mv mydir/* ./
Renaming Files/Directories
To rename a file or directory, use the mv (move) command
mv myfile myfile-newname
mv dir dirnewname
Delete Files
To delete a file
rm <filename> rm myjob.log
To delete multiple files
rm <partfilename>* rm mywork* (delete all files starting with mywork) rm *.log (delete all files ending with .log)
Delete Directories
To delete a directory (note the directory should be empty)
rmdir <directory> rmdir myworkdir
Getting help/documentation about a command
Manual command
Most (if not all) commands will have an associated manual document
man <command> man ls (get manual page on ls command) man qsub (get manual page on qsub command)
Quick help/showing command options
Most commands you can run will have a command option that will show you a list of available options and summary help. This is often the command switch --help
<command> --help ls --help qsub --help rm --help
File commands (viewing/editing/creating)
These are tools that allow you to create/modify text files via the command line or in scripts.
Touch
Creates empty files ready for writing to
touch myfile.txt
Cat
Reads files and dumps them to the console, you can redirect to a file via >
//read single file cat myfile
//read multiple files (myfiles1, myfiles2, myfiles3//0 cat myfiles*
//read multiple files and dump in a file cat myfiles* > combined.txt
//append one file to another cat file1 >> file2
Grep
Provides serach/filter operations on input (case sensitive) use regular expression. See "man grep" for more details
//read file, search for lines containing the word debug cat mylog | grep debug
//read file, search for "results returned" and output to new file cat mywork.txt | grep results\ returned > myresults.txt
Tail
To view the tail/bottom of a file. Very usefull to see the end of a log file.
//show the bottom lines of a file tail mywork.log
//show the bottom 150 lines of a file tail -n 150 myfile
//Show the bottom of a file and stays runing to show you new additional lines to the file. This will hold the terminal (i.e. it waits till you press crtl+C before you can run another command) tail -f mywork.log
Pipe output of one program into another
Using a pipe character ( | ) you can redirect the console output of one file into another.
command1 | command2
Redirect output to a file (new/append)
Using the characters > < >> << you can redirect console out put to files.
//Redirect the output of the ls command to a new file (it will over write and exsiting file of the same name. ls > mydirectorylisting.txt
//Redirect the output of a cat command and append it to an existing command cat file2 >> file1