The terminal is not scary.
But you are a beginner, and you stare at the screen wondering, what does that little cursor do? You are tired of finding overwhelming documentation online. Basics should be first and simple so you don't run away in terror.
Basics
List the contents of the current directory.
> ls
See where you are, print working directory
> pwd
Navigation
To navigate to a folder from the list, type cd and then the name of the folder. As you start typing the name of where you want to go, hit tab to use tab completions and the item will be found if it exists in that folder.
> cd .\Desktop
To go up one level type
> cd ..
magic
Files and folders
Copy a file to a new location
- cp is for copy,
- then you add the file name,
- then the path where you want to put it.
cp myfile.txt C:\Users\me\Desktop\
Moving lots of files at one via a pattern (Wildcard)
cp *.jpg C:\Users\me\Desktop
This:
- copies all of the .jpg files from the directory I am currently in,
- then puts them in the specified directory. (The path)
Rename a file
In the directory where the file is located:
- use mv command
- followed by the current file name, then a space
- then the new file name
mv .\tall_bushes.txt trees.txt
No more tall bushes, just trees. trees replaces the name tall_bushes.
Move a file
In the folder where there file is located,
- use the mv command
- followed by the name of the file and a space
- followed by the path of where you want it to go.
mv .\trees.txt C:\Users\me\Desktop
We are moving trees.txt to the path specified.
Find a matching document
Here, we are searching for all of the documents in that path that contain _document.txt. Probably handy. The asterisk means all, everything, or whatever else.
mv *_document.text C:\Users\me\Documents
Making new folders and directories
Create a new folder using mkdir and your folder name.
mkdir new_folder
If you want to put a space in the file name, use quotes.
mkdir 'new folder'
Bash
To copy a directory and all of its contents, we must use -r
$ cp -r "Bird Pictures" C:/Users/me/Desktop/
To remove a folder and all of its contents we use rm -r
$ rm -r "Bird Pictures"
PowerShell
Copy a directory
To copy a directory and all of its contents, we must use -recurse
cp "Bird Pictures" C:\Users\me\Desktop -recurse -verbose
Remove File
To remove files we use rm or remove. Be warned that these do not go to the recycling bin, but are deleted forever.
rm ~\lame_document.txt
Remove Folder
rm ~\lame_folder
You can use -recurse if you do not want to be asked if you are sure:
rm ~\lame_folder -recurse
You can force a file to be removed if you have the permissions, with -force
File Viewing
Powershell and Bash
To view the contents of a file in the terminal, we can use cat
cat .\important_document.txt
The most important document on the planet!
To view the first few lines in a document we can use the head command
cat casino_winnings.txt -Head 10
I don't win at the casino, so perhaps this isn't a good use case. XD
To see the last few lines in a file:
cat tallest_buildings.txt -tail 5
A much better use case.
Powershell
If we have a large document we want to view in the terminal, we can use the more command
more .\Gigantic_book.txt
You can use the enter key to go through the document line by line. Spacebar will move the document one page at a time, a page being the space in your terminal.
If you want to escape about of more, too bad.
Just kidding, press the q key to return to shell.
To open a file with an app, you can can type start, then the name of the app, then the name of the file.
> start notepad awesome_document.txt
Bash To edit a file from the terminal, we can use nano. Type nano and then the file name.
$ nano fuzzy_kitty
Ignore my grammar in the screenshot lol.
Bash
To view a very large document in bash we can use the less command:
$ less 20000_leagues_under_the_sea.txt
less in bash is more versatile than more. You can use:
- up and down keys
- page up, page down
- g moves to the beginning of a file
- G moves to the end
- /word_search allows you to search for a word or phrase. We type in a slash and then what we want to search for.
$ /huge whale
That's enough to poke around. Have fun!
Top comments (1)
This article is amazing!