Difference between revisions of "Get acquainted with the command line"
Line 1: | Line 1: | ||
=Unix Cheat Sheet= | =Unix Cheat Sheet= | ||
Unix commands are useful to get familiar with in order to use software with [[Command line|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, a user can "pipe" commands, use wildcards, and write scripts that automate a wide variety of tasks. | |||
==General commands== | ==General commands== | ||
Line 19: | Line 21: | ||
==Change to directory== | ==Change to directory== | ||
cd {dirname} There must be a space between. | <code>cd {dirname}</code> There must be a space between.<br> | ||
cd ~ Go back to home directory, useful if you're lost. | <code>cd ~</code> Go back to home directory, useful if you're lost.<br> | ||
cd .. Go back one directory. | <code>cd ..</code> Go back one directory. | ||
==Make a new directory== | ==Make a new directory== | ||
mkdir {dirname} | <code>mkdir {dirname}</code> | ||
==Remove a directory== | ==Remove a directory== | ||
rmdir {dirname} Only works if {dirname} is empty. | <code>rmdir {dirname}</code> Only works if {dirname} is empty.<br> | ||
rm -r {dirname} Remove all files and subdirs. Careful! | <code>rm -r {dirname}</code> Remove all files and subdirs. <span style="background-color:yellow;">Careful!</span><br> | ||
==Copy a file or directory== | ==Copy a file or directory== | ||
cp {file1} {file2} | <code>cp {file1} {file2}</code><br> | ||
cp -r {dir1} {dir2} Recursive, copy directory and all subdirs | <code>cp -r {dir1} {dir2}</code> Recursive, copy directory and all subdirs<br> | ||
cat {newfile} >> {oldfile} 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== | ||
mv {oldfile} {newfile} Moving a file and renaming it are the same thing | <code>mv {oldfile} {newfile}</code> Moving a file and renaming it are the same thing<br> | ||
mv {oldname} {newname} | <code>mv {oldname} {newname}</code> | ||
==Delete a file== | ==Delete a file== | ||
rm {filespec} ? and * wildcards work like DOS should. "?" is any character; "*" is any string of characters. | <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! | ||
ls {filespec} | |||
rm {filespec} Good strategy: first list a group to make sure it's what's you think... then delete it all at once. | <code>rm {filespec}</code><code>?</code> and <code>*</code> wildcards work like DOS should. "?" is any character; "*" is any string of characters. | ||
<br> | |||
<code>ls {filespec}</code><br> | |||
<code>rm {filespec}</code> Good strategy: first list a group to make sure it's what's you think... then delete it all at once. | |||
==View a text file== | ==View a text file== | ||
more {filename} View file one screen at a time | <code>more {filename}</code> View file one screen at a time<br> | ||
less {filename} Like more, with extra features | <code>less {filename}</code> Like more, with extra features<br> | ||
cat {filename} View file, but it scrolls | <code>cat {filename}</code> View file, but it scrolls<br> | ||
cat {filename} | more View file one screen at a time | <code>cat {filename} | more</code> View file one screen at a time<br> | ||
==Edit a text file.== | ==Edit a text file.== | ||
Line 54: | Line 59: | ||
==Create a text file.== | ==Create a text file.== | ||
cat > {filename} Enter your text (multiple lines with enter are ok) and press control-d to save | <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> | |||
Compare two files | <code>nano {filename}</code> Create some text and save it in a file<br> | ||
diff {file1} {file2} Show the differences | |||
sdiff {file1} {file2} Show files side by side | ===Compare two files=== | ||
Other text commands | <code>diff {file1} {file2}</code> Show the differences<br> | ||
grep '{pattern}' {file} Find regular expression in file | <code>sdiff {file1} {file2}</code> Show files side by side<br> | ||
spell {file} Display misspelled words | |||
wc {file} Count words in file | ===Other text commands=== | ||
wc -l {file} Count the number of lines in a file | <code>grep '{pattern}' {file}</code> Find regular expression in file<br> | ||
Make an Alias | <code>spell {file}</code> Display misspelled words<br> | ||
alias {name}='{command}' Put the command in 'single quotes'. More useful in your .bashrc file | <code>wc {file}</code> Count words in file<br> | ||
Wildcards and Shortcuts | <code>wc -l {file}</code> Count the number of lines in a file<br> | ||
* Match any string of characters, eg page* gets page1, page10, and page.txt | |||
? Match any single character, eg page? gets page1 and page2, but not page10 | ==Make an Alias== | ||
[...] Match any characters in a range, eg page[1-3] gets page1, page2, and page3 | <code>alias {name}='{command}'</code> Put the command in 'single quotes'. More useful in your .bashrc file<br> | ||
~ Short for your home directory, eg cd ~ will take you home, and rm -r ~ will destroy it | |||
. The current directory | ==Wildcards and Shortcuts== | ||
.. One directory up the tree, eg ls ... | <code>*</code> Match any string of characters, eg page* gets page1, page10, and page.txt<br> | ||
Pipes and Redirection | <code>?</code> Match any single character, eg page? gets page1 and page2, but not page10<br> | ||
{command} > {file} Redirect output to a file, eg ls > list.txt writes directory to file | <code>[...]</code> Match any characters in a range, eg page[1-3] gets page1, page2, and page3<br> | ||
{command} >> {file} Append output to an existing file, eg cat update >> archive adds update to end of archive | <code>~</code> Short for your home directory, eg cd ~ will take you home, and rm -r ~ will destroy it<br> | ||
{command} < {file} Get input from a file, eg sort < file.txt | <code>.</code> The current directory<br> | ||
{command} < {file1} > {file2} Get input from file1, and write to file2, eg sort < old.txt > new.txt sorts old.txt and saves as new.txt | <code>..</code> One directory up the tree, eg ls ...<br> | ||
{command} | {command} Pipe one command to another, eg ls | more gets directory and sends it to more to show it one page at a time | |||
System info | ==Pipes and Redirection== | ||
date Show date and time | You pipe a command to another command, and redirect it to a file. | ||
df Check system disk capacity | |||
du Check your disk usage and show bytes in each directory | <code>{command} > {file}</code> Redirect output to a file, eg ls > list.txt writes directory to file<br> | ||
du -h Check your disk usage in a human readable format | <code>{command} >> {file}</code> Append output to an existing file, eg cat update >> archive adds update to end of archive<br> | ||
printenv Show all environmental variables | <code>{command} < {file}</code> Get input from a file, eg sort < file.txt<br> | ||
uptime Find out system load | <code>{command} < {file1} > {file2}</code> Get input from file1, and write to file2, eg sort < old.txt > new.txt sorts old.txt and saves as new.txt<br> | ||
w Who's online and what are they doing? | <code>{command} | {command}</code> Pipe one command to another, eg ls | more gets directory and sends it to more to show it one page at a time<br> | ||
top Real time processor and memory usage | |||
==System info== | |||
<code>date</code> Show date and time<br> | |||
<code>df</code> Check system disk capacity<br> | |||
<code>du</code> Check your disk usage and show bytes in each directory<br> | |||
<code>du -h</code> Check your disk usage in a human readable format<br> | |||
<code>printenv</code> Show all environmental variables<br> | |||
<code>uptime</code> Find out system load<br> | |||
<code>w</code> Who's online and what are they doing?<br> | |||
<code>top</code> Real time processor and memory usage | |||
==For fun== | |||
<code>wall {message}</code> Write a message to all the other users currently logged in | |||
[[Category: Cookbook]] | [[Category: Cookbook]] |
Revision as of 21:10, 21 October 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, 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}
Type man rm to read the manual for the rm command.
whoami
Shows which user account is being used in a session
whatis {command}
Give short description of command.
List a directory
ls
shows the contents of a directory.
ls {path}
It's ok to combine attributes, eg ls -laF
gets a long listing of all files with types.
ls {path_1} {path_2}
List both {path_1} and {path_2}.
ls -l {path}
Long listing, with date, size and permisions.
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 subdirs.
ls {path} | more
Show listing one screen at a time.
Change to directory
cd {dirname}
There must be a space between.
cd ~
Go back to home directory, useful if you're lost.
cd ..
Go back one directory.
Make a new directory
mkdir {dirname}
Remove a directory
rmdir {dirname}
Only works if {dirname} is empty.
rm -r {dirname}
Remove all files and subdirs. Careful!
Copy a file or directory
cp {file1} {file2}
cp -r {dir1} {dir2}
Recursive, copy directory and all subdirs
cat {newfile} >> {oldfile}
Append newfile to end of oldfile
Move (or rename) a file
mv {oldfile} {newfile}
Moving a file and renaming it are the same thing
mv {oldname} {newname}
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 {filespec}
?
and *
wildcards work like DOS should. "?" is any character; "*" is any string of characters.
ls {filespec}
rm {filespec}
Good strategy: first list a group to make sure it's what's you think... then delete it all at once.
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.
gedit
or nano
{filename}
Basic text editor
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
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, eg page* gets page1, page10, and page.txt
?
Match any single character, eg page? gets page1 and page2, but not page10
[...]
Match any characters in a range, eg page[1-3] gets page1, page2, and page3
~
Short for your home directory, eg cd ~ will take you home, and rm -r ~ will destroy it
.
The current directory
..
One directory up the tree, eg ls ...
Pipes and Redirection
You pipe a command to another command, and redirect it to a file.
{command} > {file}
Redirect output to a file, eg ls > list.txt writes directory to file
{command} >> {file}
Append output to an existing file, eg cat update >> archive adds update to end of archive
{command} < {file}
Get input from a file, eg sort < file.txt
{command} < {file1} > {file2}
Get input from file1, and write to file2, eg sort < old.txt > new.txt sorts old.txt and saves as new.txt
{command} | {command}
Pipe one command to another, eg 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
wall {message}
Write a message to all the other users currently logged in