Difference between revisions of "Get acquainted with the command line"
(30 intermediate revisions by one other user not shown) | |||
Line 11: | Line 11: | ||
==List a directory== | ==List a directory== | ||
<code>ls</code> | <code>ls</code> lists the contents of a directory. It can be used with many options and attributes to show particular settings given to files, including file permissions. | ||
It's ok to combine attributes, e.g. <code>ls -laF</code> gets a long listing of all files with types. | It's ok to combine attributes, e.g. <code>ls -laF</code> gets a long listing of all files with types. | ||
<code>ls {path}</code> List the contents of the path<br> | <code>ls {path}</code> List the contents of the path<br> | ||
<code>ls {path_1} {path_2}</code>List both {path_1} and {path_2}<br> | <code>ls {path_1} {path_2}</code>List both <code>{path_1}</code> and <code>{path_2}</code><br> | ||
<code>ls -l {path}</code> Long listing, with date, size and permissions<br> | <code>ls -l {path}</code> Long listing, with date, size and permissions<br> | ||
<code>ls -a {path}</code> Show all files, including important .dot files that don't otherwise show<br> | <code>ls -a {path}</code> Show all files, including important .dot files that don't otherwise show<br> | ||
Line 30: | Line 30: | ||
==Make a new directory== | ==Make a new directory== | ||
<code>mkdir {dirname}</code> | <code>mkdir {dirname}</code> Make a directory and name it | ||
==Remove a directory== | ==Remove a directory== | ||
Line 37: | Line 37: | ||
==Copy a file or directory== | ==Copy a file or directory== | ||
<code>cp {file1} {file2}</code><br> | <code>cp {file1} {file2}</code> Copy file1 and call it file2<br> | ||
<code>cp -r {dir1} {dir2}</code> Recursive, copy directory and all | <code>cp -r {dir1} {dir2}</code> Recursive, copy directory and all subdirectories<br> | ||
<code>cat {newfile} >> {oldfile}</code> Append newfile to end of oldfile | <code>cat {newfile} >> {oldfile}</code> Append newfile to end of oldfile | ||
==Move (or rename) a file== | ==Move (or rename) a file== | ||
<code>mv { | <code>mv {file1} folder2/{file1}</code> Move file1 to a subdirectory called folder2<br> | ||
<code>mv {oldname} {newname}</code> | <code>mv {oldname} {newname}</code> In UNIX, moving a file and renaming it are the same thing, much like moving or renaming pages on a wiki<br> | ||
==Delete a file== | ==Delete a file== | ||
<span style="background-color:yellow;">Warning:</span> Be very careful when using the <code>rm</code> command. Anything removed with it will be gone forever - there is no undo! | <span style="background-color:yellow;">Warning:</span> Be very careful when using the <code>rm</code> command. Anything removed with it will be gone forever - there is no undo! | ||
<code>rm { | <code>rm {file1}</code> Removes the file named file1 | ||
==View a text file== | ==View a text file== | ||
Line 60: | Line 57: | ||
==Edit a text file== | ==Edit a text file== | ||
<code>nano {filename}</code>Basic text editor | <code>nano {filename}</code> Basic text editor for Unix-like systems, including Mac<br> | ||
<code>gedit {filename}</code> A text editor included with GNOME systems | |||
==Create a text file | ==Create a text file== | ||
<code>touch {filename}</code> Create a file and save it. It will remain empty until you edit it<br> | <code>touch {filename}</code> Create a file and save it. It will remain empty until you edit it<br> | ||
<code>cat > {filename}</code> Enter your text (multiple lines with enter are ok) and press control-d to save<br> | <code>cat > {filename}</code> Enter your text (multiple lines with enter are ok) and press control-d to save<br> | ||
Line 73: | Line 71: | ||
===Other text commands=== | ===Other text commands=== | ||
<code>grep '{pattern}' {file}</code> Find regular expression in file<br> | <code>grep '{pattern}' {file}</code> Find regular expression in file<br> | ||
<code>spell {file}</code> Display misspelled words<br> | <code>spell {file}</code> Display misspelled words in Linux - for Mac install <code>ispell</code> with homebrew<br> | ||
<code>wc {file}</code> Count words in file<br> | <code>wc {file}</code> Count words in file<br> | ||
<code>wc -l {file}</code> Count the number of lines in a file<br> | <code>wc -l {file}</code> Count the number of lines in a file<br> | ||
==Make an Alias== | ==Make an Alias== | ||
<code>alias {name}='{command}'</code> Put the command in 'single quotes'. More useful in your .bashrc file<br> | <code>alias {name}='{command}'</code> Put the command in 'single quotes'. More useful in your <code>.bashrc</code> file<br> | ||
==Wildcards and Shortcuts== | ==Wildcards and Shortcuts== | ||
<code>*</code> Match any string of characters, | <code>*</code> Match any string of characters, e.g. <code>page*</code> gets <code>page1</code>, <code>page10</code>, and <code>page.txt</code><br> | ||
<code>?</code> Match any single character, | <code>?</code> Match any single character, e.g. <code>page?</code> gets <code>page1</code> and <code>page2</code>, but not <code>page10</code><br> | ||
<code>[...]</code> Match any characters in a range, | <code>[...]</code> Match any characters in a range, e.g. <code>page[1-3]</code> gets <code>page1</code>, <code>page2</code>, and <code>page3</code><br> | ||
<code>~</code> Short for your home directory, | <code>~</code> Short for your home directory, e.g. <code>cd ~</code> will take you home, and <code>rm -r ~</code> will destroy it<br> | ||
<code>.</code> The current directory<br> | <code>.</code> The current directory<br> | ||
<code>..</code> One directory up the tree | <code>..</code> One directory up the tree | ||
<code>ctrl + r</code> displays (reverse-i-search) prompt. Start typing and use <code>ctrl + r</code> to cycle results. <code>enter</code> will execute selected command or <code>esc</code> to exit. | |||
==Pipes and Redirection== | ==Pipes and Redirection== | ||
You pipe a command to another command, and redirect it to a file. The pipe symbol is <code>|</code>. | You pipe a command to another command, and redirect it to a file. The pipe symbol is <code>|</code>. | ||
<code>{command} > {file}</code> Redirect output to a file, | <code>{command} > {file}</code> Redirect output to a file, e.g. <code>ls > list.txt</code> writes directory to file<br> | ||
<code>{command} >> {file}</code> Append output to an existing file, | <code>{command} >> {file}</code> Append output to an existing file, e.g. <code>cat update >> archive</code> adds update to end of archive<br> | ||
<code>{command} < {file}</code> Get input from a file, | <code>{command} < {file}</code> Get input from a file, e.g. <code>sort < file.txt</code></br> | ||
<code>{command} < {file1} > {file2}</code> Get input from file1, and write to file2, | <code>{command} < {file1} > {file2}</code> Get input from file1, and write to file2, e.g. <code>sort < old.txt > new.txt</code> sorts <code>old.txt</code> and saves as <code>new.txt</code><br> | ||
<code>{command} | {command}</code> Pipe one command to another, | <code>{command} | {command}</code> Pipe one command to another, e.g. <code>ls | more</code> gets directory and sends it to more to show it one page at a time<br> | ||
==System info== | ==System info== | ||
Line 108: | Line 108: | ||
==For fun== | ==For fun== | ||
<code>wall {message}</ | If you are sharing a computer with other users who are also logged in at the same time, write a message using the <code>wall</code> command: | ||
<syntaxhighlight lang="bash"> | |||
wall {message} | |||
</syntaxhighlight> | |||
[[Category:Cookbook]] | [[Category:Cookbook]] | ||
[[Category: | [[Category:Command line]] |
Latest revision as of 17:42, 9 December 2021
Unix Cheat Sheet
Unix commands are useful to get familiar with in order to use software with command line interfaces. Many computers have a terminal program through which users may use an operating system. These commands can also be used on Unix-like systems, such as Mac OS, and a wide variety of Linux distributions, including Debian and Ubuntu. In a Unix shell session, a user can "pipe" commands, use wildcards, and write scripts that automate a wide variety of tasks.
General commands
pwd
Print working directory - show where you are as full path. Useful if you're lost or exploring
man {command}
Shows the manual. For example, type man rm
to read the manual for the rm
command
whoami
Shows which user account is being used in a current session
whatis {command}
Give short description of command
List a directory
ls
lists the contents of a directory. It can be used with many options and attributes to show particular settings given to files, including file permissions.
It's ok to combine attributes, e.g. ls -laF
gets a long listing of all files with types.
ls {path}
List the contents of the path
ls {path_1} {path_2}
List both {path_1}
and {path_2}
ls -l {path}
Long listing, with date, size and permissions
ls -a {path}
Show all files, including important .dot files that don't otherwise show
ls -F {path}
Show type of each file. "/" = directory, "*" = executable
ls -R {path}
Recursive listing, with all sub-directories
Change to directory
cd
is to change directory.
cd {dirname}
There must be a space between the command and the {dirname}
cd ~
Go back to home directory, useful if you're lost
cd ..
Go back one directory
Make a new directory
mkdir {dirname}
Make a directory and name it
Remove a directory
rmdir {dirname}
Only works if the directory is empty
rm -r {dirname}
Remove all files and sub-directories. Careful!
Copy a file or directory
cp {file1} {file2}
Copy file1 and call it file2
cp -r {dir1} {dir2}
Recursive, copy directory and all subdirectories
cat {newfile} >> {oldfile}
Append newfile to end of oldfile
Move (or rename) a file
mv {file1} folder2/{file1}
Move file1 to a subdirectory called folder2
mv {oldname} {newname}
In UNIX, moving a file and renaming it are the same thing, much like moving or renaming pages on a wiki
Delete a file
Warning: Be very careful when using the rm
command. Anything removed with it will be gone forever - there is no undo!
rm {file1}
Removes the file named file1
View a text file
more {filename}
View file one screen at a time
less {filename}
Like more, with extra features
cat {filename}
View file, but it scrolls
cat {filename} | more
View file one screen at a time
Edit a text file
nano {filename}
Basic text editor for Unix-like systems, including Mac
gedit {filename}
A text editor included with GNOME systems
Create a text file
touch {filename}
Create a file and save it. It will remain empty until you edit it
cat > {filename}
Enter your text (multiple lines with enter are ok) and press control-d to save
nano {filename}
Create some text and save it in a file
Compare two files
diff {file1} {file2}
Show the differences
sdiff {file1} {file2}
Show files side by side
Other text commands
grep '{pattern}' {file}
Find regular expression in file
spell {file}
Display misspelled words in Linux - for Mac install ispell
with homebrew
wc {file}
Count words in file
wc -l {file}
Count the number of lines in a file
Make an Alias
alias {name}='{command}'
Put the command in 'single quotes'. More useful in your .bashrc
file
Wildcards and Shortcuts
*
Match any string of characters, e.g. page*
gets page1
, page10
, and page.txt
?
Match any single character, e.g. page?
gets page1
and page2
, but not page10
[...]
Match any characters in a range, e.g. page[1-3]
gets page1
, page2
, and page3
~
Short for your home directory, e.g. cd ~
will take you home, and rm -r ~
will destroy it
.
The current directory
..
One directory up the tree
ctrl + r
displays (reverse-i-search) prompt. Start typing and use ctrl + r
to cycle results. enter
will execute selected command or esc
to exit.
Pipes and Redirection
You pipe a command to another command, and redirect it to a file. The pipe symbol is |
.
{command} > {file}
Redirect output to a file, e.g. ls > list.txt
writes directory to file
{command} >> {file}
Append output to an existing file, e.g. cat update >> archive
adds update to end of archive
{command} < {file}
Get input from a file, e.g. sort < file.txt
{command} < {file1} > {file2}
Get input from file1, and write to file2, e.g. sort < old.txt > new.txt
sorts old.txt
and saves as new.txt
{command} | {command}
Pipe one command to another, e.g. ls | more
gets directory and sends it to more to show it one page at a time
System info
date
Show date and time
df
Check system disk capacity
du
Check your disk usage and show bytes in each directory
du -h
Check your disk usage in a human readable format
printenv
Show all environmental variables
uptime
Find out system load
w
Who's online and what are they doing?
top
Real time processor and memory usage
For fun
If you are sharing a computer with other users who are also logged in at the same time, write a message using the wall
command:
wall {message}