Basic linux command reference
Output and redirection
Redirect the output of a command into a file (overwriting the file if it already exists) by adding > /path/to/export/file
at the end of your command
Redirect the output of a command into a file (appending the contents to the end of already existing content if there is any) by adding » /path/to/export/file
at the end of your command
If you want to redirect the output of your command to the input of another command, use the pipe symbol |
Examples:
grep something /foo/bar > /tmp/somthing.txt
grep something /foo/bar » /tmp/something.txt
grep something /foo/bar |wc -l
Find a string within a file with grep
grep
looks for a specific string within a file or series of files. The files argument can be a file, a path to a file and /
or wildcards to get to the files you want to search.
grep <string> <files>
zgrep
works the same way, but will work on gzipped files, automatically uncompressing them on the fly and searching for the string in their compressed contents.
If you want to add a regular expression as a search string, you will need to specify the -E
option
grep -E <regex> <files>
Output the contents of a file to the console
cat <fileToDisplay>
Don't do this with a binary file. s̸t̴r̸a̸n̸g̷e̷ t̶h̴i̷n̴g̷s̶ ̵w̵i̶l̶l̷ ̴h̵a̴p̶p̶e̶n̶.̷.̷.̵
Count words and rows
Take the output of any command that outputs text and pipe it into wc
(word count). The -l
option counts lines, the -c
option counts characters.
ls -Al | wc -l
ls -Al | wc -c
Browsing around the file system
cd
: Change directorymkdir
: Make directoryrmdir
: Remove directoryrm
: Remove filels
: List files in a directorydir
: Similar to ls but can be used to generate a simpler outputpwd
: Print Working Directory: Shows you where you are in the file system
$ cd / $ cd ~ $ cd /tmp $ mkdir test $ rmdir test $ pwd /tmp $ ls com.apple.launchd.Lq7iT6vH0t powerlog
ls and arguments
You can add all sorts of arguments to the ls
command to make it output different information in different ways, for example, the example above shows ls
displaying 2 files in the /tmp
folder, but you can see much more. Generally use the options -Altr
: All files (except . and ..), long format, time modified, reverse search on the time so newest file at the bottom, and you could also add h
to the string, so display file sizes in humanly readable format (in Bytes, Kilo, Mega, Giga… bytes):
$ ls private project.properties project.xml $ ls -Altr total 16 -rw-r--r-- 1 daniel.page mygroup 166 Jan 13 15:04 project.properties -rw-r--r-- 1 daniel.page mygroup 314 Jan 13 15:04 project.xml drwxr-xr-x 4 daniel.page mygroup 128 Jan 13 15:24 private $ ls -Altrh total 16 -rw-r--r-- 1 daniel.page mygroup 166B Jan 13 15:04 project.properties -rw-r--r-- 1 daniel.page mygroup 314B Jan 13 15:04 project.xml drwxr-xr-x 4 daniel.page mygroup 128B Jan 13 15:24 private
cd and special directories
- Get back Your personal directory - your home directory: use the
~
alias. From wherever you are in a file system, typecd ~
and you will be transported back to your home directory. - Go back a level in the directory: Each directory has 2 system files:
.
and..
- The single dot represents the current directory, and the double dot, the current directory's parent. To go up one level, rather than entering the full path to the parent directory (that will also work), just typecd ..
(you can also docd .
but all this will do is change you back to the current directory you are currently in right now) - The special directories
.
and..
will not be displayed if you run ls with theA
argument. They will display if you replace it by thea
argument.
Manual pages
If you don't remember how a command works or you want more information on a command, use the man
pages to access the manual for that command. Enter man
followed by the name of the tool you want to check out, for example man ls
Scroll up and down with your arrow or page up/down keys to view and q
to quit.