Linux

From PHYSpedia
Revision as of 08:58, 7 February 2013 by Cclark (talk | contribs) (Useful Commands)

Jump to: navigation, search

Misc Links

These topics have their own pages.

Getting Started

Gnuplot

Common C, C++, FORTRAN Libraries

The Command Line

Useful Commands

The following is a (short) list of useful command when manipulating data in text files. Some common options are listed, but may not be explained. For full descriptions of each command, see the man page. stdin, stdout, and stderr refer to standard input, output and error, respectively. ====File Management: Creating, Viewing, Moving, Deleting, Etc.

ls [dir]
list the contents of a directory to stdout
if no directory is given, the contents of the current directory are listed
common options: -l, -a
cd [dir]
change current working directory to a directory. i.e. go into a directory
used to navigate the filesystem
mkdir dir
make directory named 'dir'
rm file1 [file2 [...] ]
remove one or more files
if trying to remove a directory, the -r option must be given
mv source [source2 [...] ] dest
move a file or files. if multiple files are given, then the last argument must be a directory.
examples
cat [file1 [ file2 [...] ] ]
print text file(s) to stdout. if multiple files are given, their text is concatenated
useful for starting a pipeline of commands from the contents of a text file


Processing Text Files

grep "pattern" [ file1 [ file2 [...] ] ]
find text matching 'pattern' in files, or from stdin, and print matching lines to stdout
useful for finding a file containing a specific configuration options
useful for filtering output of a program to only those lines that are interesting
echo [args...]
print arguments to stdout
useful for starting a pipeline of commands from a variable's value
useful for printing output to the terminal from inside a script
sort
read from stdin, sort lines, and write to stdout
common options: -k, -t, -n, -g
cut
read from stdin and write selected columns to stdout.
common options: -d, -f
column
read from stdin and write to stdout such that columns are aligned
common options: -t
head [file]
read from stdin, or from a file, and print the first 10 lines
useful for taking a peak at the contents of a large file
useful for limitng the output of a pipeline
common options: -n
tail [file]
read from stdin, or from a file, and print the last 10 lines
similar uses to head
can be used with head to print a single line, or range of lines, in a file
common options: -n
sed 's/old/new/g' [file]
read from stdin, or from a file, and replace all instances of 'old' with 'new', then print to stdout

Pipes

Pipes allow you to use multiple commands together. Any command that reads input from stdin and writes to stdout can be used in a pipe.

Shell Scripts

Shell scripts are just text files that contain a list of commands for the shell to run. Anything you can run at the command line can be ran in a shell script. To create a shell script, you must do two things.

  1. create a text file with the comment #! /bin/bash on the first line.
  2. make the text file "executable" with the chmod command.

Here is an example of a simple "hello world" script.


#! /bin/bash

echo "hello world"

If you save this in a file named 'hello.sh', then to run it you must first make it executable.


user@host:~$ chmod +x hello.sh
user@host:~$ ./hello.sh

You may find some more information on the scripting page.