- Shortcuts: . (present directory), .. (parent directory), ~ (home directory).
- The tree command is a good way to get a bird’s-eye view of the filesystem tree. Use tree -d to view just the directories and to suppress listing file names.
A shell is a program that runs operating system commands. In general, there are two key differences between shell scripts and more complex programs. First, shells are designed to handle short, simple tasks.They lack the rigid structure and semantic checking of a high-level language. Shell scripts can be written quickly because they assume the programmer knows what he or she is doing, and for short scripts, this is usually true. Second, shells always execute their scripts slowly. Although most shells perform some kind of pre-processing to speed up execution, they still interpret and carry out one line at time. High-level languages are faster because they almost always translate a program into machine language to gain the best performance. When tackling any programming problem, it’s essential to choose the right tool for the job. Developing large projects as shell scripts will mean your project will run slowly and be difficult to maintain. Developing scripts in a high-level language will lead to a longer and more costly development time. (source)
? | Matches any single character |
* | Matches any string of characters |
[set] | Matches any character in the set of characters, for example [adf] will match any occurrence of "a", "d", or "f" |
[!set] | Matches any character not in the set of characters |
$ do_something < input-file
$ do_something > output-file (because stderr is not the same as stdout, error messages will still be seen on the terminal windows)
$ do_something >> append-to-output-file
When commands are executed, by default there are three standard file streams (or descriptors) always open for use:
stdin (descriptor 0), stdout (descriptor 1) and stderr (descriptor 2).
If other files are opened in addition to these three, which are opened by default, they will start at file descriptor 3.
- To redirect stderr to a separate file: $ do_something 2> error-file
- Shorthand notation to put anything written to stderr in the same place as stdout:
$ do_something > all-output-file 2>&1 (bash-specific: $ do_something >& all-output-file)
$ cat > myfile << EOF writes everything typed into myfile until EOF is typed (can use anything instead of EOF).
/dev/null is a special file that discards all data that gets written to it.
To ignore the standard output stream but printing any errors on the console:
$ find / > /dev/null
<<< (in newer versions of bash) can be used to redirect content of variables:
$ TEST="John"
$ wc -m <<< $TEST
5
$ w 3:18pm up 9 days, 20:33, 3 users, load average: 0.64, 0.66, 0.64 User tty login@ idle JCPU PCPU what dhu ttyp6 2:12pm 4:28 8:01 8:01 csh mchung console 9:57am 5:10 sh bgill ttyp7 1:32pm 19 bash
$ echo "test" | tr [:lower:] [:upper:]
$ echo "John123" | tr -d [:digit:]
$ echo "This       is   for     testing" | tr -s [:space:]
$ echo "my username is 432234" | tr -cd [:digit:]
$ tr -cd [:print:] < FILENAME
TEST
John
This is or testing
432234
[Remove all non-printable characters]
$ ls -l | cut -d " " -f 3-
    All fields of ls -l except the first two
$ at now + 5 minutes # Man page of 'at' describes different time formats at> echo "test" at> ^d
$ file test test: directory