What is command line coding?
The command line is a text interface to your operating system, it takes in commands through a shell interface and passes these commands to your computer to be actioned, the command line is also referred to as `Terminal` in some operating systems.
From the command line, you can automatically rename hundreds of files easily, navigate through files and folders on your computer, perform system wide search for a file, all without having to open up file finders or any graphic interface.
How do I get to the command line on a Windows?
In Windows 10, open the Start Menu and go to the Windows System shortcuts folder, you can find a shortcut for the Command Prompt, click on that to open it up.
How do I get to the command line on a Mac?
The command line in a mac is a program called Terminal, you can open it up by simply using CMD + Spacebar, then type `terminal` into the search bar, the terminal will open up for you, it is in here that you will enter all commands.
Below is a list of commonly used commands:
The Tilda icon ( ~ )
Tilda ( ~ ) is the icon for your home directory, so the above code is basically saying change into my home directory.
pwd - print working directory
This prints the current working directory, very useful whilst working in the terminal and you want to remind yourself of what directory you are currently in.
cat - print out content in your terminal
The above will print out right in your terminal screen all the contents of the file cities.txt
> - Move content from left to right
cat cities.txt > country.txt
This takes the output from the file on the left and puts it into the file on the right, using the ' > ' symbol.
wc -w word count
wc -w countries.txt
The above prints the number of words in a file.
So this will take you out of the current directory, first check `pwd` to see where you are, then try `cd..` notice you have up a level in your directory tree?
cp - make a copy of a file
cp shopping_list.txt another_shopping_list.txt
This will create a copy of shopping_list.txt and name it another_shopping_list.txt
cp * images/
The wildcard * in this case will copy all the files in the current directory (pwd) and put copies of the files in another directory called images.
cp images/* backup/
By using the * wildcard like above, it will copy all the files in images folder and copy them into another folder called backup.
env - environment
The env command stands for "environment", it returns a list of the environment variables for the current user.
env | grep PATH
env | grep PATH command will displays the value of a single environment variable.
export STUDENT="John Doe"
export makes the variable to be available for the current terminal session, closing your terminal or opening a new tab wil delete the STUDENT variable created.
The above will print the value of the STUDENT variable.
grep - global regular expression print
grep "London" cities.txt
grep stands for “global regular expression print”. It searches a file and returns a matching pattern.
grep -i "London" cities.txt
Passing the i flag lets you search and find results in a case insensitive way, this will return results matching 'London' or 'london'
grep -R Ford ~/cars
grep -R searches all files in a directory and return the filename and lines containing the search word, the -R stands for "recursive", this will search folders within folders until it finds all matching words.
grep -Rl Ford ~/cars
grep -Rl searches all files in a directory and only returns the filenames that match, it does not return the line in the file.
$HOME - path to home directory
HOME is a variable that prints the path of the home directory
ls - list
This will list all the files in the current working dirctory
List all files in current working directory including all the hidden files
List all files in current working directory by the time they were last modified
touch - creating new file
Touch is used to create a new file.
mkdir - make directory
Create a new directory called songs
mv - moving files
mv song_list.txt songs/
Move the song_list.txt file into a directory called songs
nano - text editor
Open a file using nano text editor, to close this file you will need to use the ctrl + x keyboard combination.
rm - remove
This will delete a file called song_list.txt
rm -rf cars
Unfortunately just rm will not work for directories, but using -rf ( recursive force) will remove folders as well.
sed - stream editor
sed 's/London/Rio/' cities.txt
sed stands for “stream editor”. It accepts standard input and modifies it based on an expression, the expression 's/London/Rio/' will substitute London for Rio within the cities.txt and return the result.