Working with Terminal in Linux - Part 2
learn to do basic operations in Linux using terminal like a pro
Table of contents
- What will you learn?
- Let's kick that engine back up and turn that volume up, shall we?
- How to copy, move ( cut ) and remove ( delete ) using Terminal :
- commands - cp, mv and rm
- Copy - cp
- Copying a directory:
- Move - mv ( cut )
- How to Create and Delete Files and Directories using Terminal
- Creating Files
- cat command
- Creating Directories ( Folder )
- NOTE: It is best that you don't use special characters or spaces in a file name or directory name as you did in Windows, except hyphens.
- If you want you can do it but it will make your workflow slow as you'll have to escape those characters using \ while working in the terminal.
- Removing ( or deleting ) Files and Directories in terminal
- Concluding:
- Thanks for Reading This!
Continuation of: Working with Terminal in Linux - Part 1
NOTE: BLOG TARGETS ABSOLUTE BEGINNERS
What will you learn?
How to copy, and cut ( or move ) stuff.
Different options to create and delete ( remove ) files and directories ( folders ).
Concluding
Let's kick that engine back up and turn that volume up, shall we?
How to copy, move ( cut ) and remove ( delete ) using Terminal :
commands - cp, mv and rm
Copy - cp
cp is the command for copy.
There's no file in the Documents directory when we check its content by using the ls command.
Then we use the cp command to copy a file named file.txt from the current directory to the Documents directory.
Then we ran the ls command on the Documents directory once more, now we see there's that file.txt present in it, which confirms that the file was copied successfully.
Copying a directory:
We use an option ( -r for recursive copying ) with cp command before specifying the directory that we want to copy.
As you can see, Pictures was empty earlier, but now it has the Documents directory in it.
-r option stands for recursive copying which means that the directory and the contents in it recursively get copied to the specified path.
NOTE: Copying a directory without an -r ( also called a switch ) option throws an error:
That's it for cp. Let's learn to cut or move files and directories using the terminal next.
Move - mv ( cut )
- mv is the command used to move ( or cut and paste ) a file or directory.
The file "file.txt" was cut from the current directory and pasted into the public directory.
It works the same for directories, it doesn't require any additional option like -r in the cp command.
mv command to rename a file or directory:
mv command can be used to rename a file or directory as well.
The syntax is quite simple too. Just use the command, then specify the name ( with relative or full path ) of the file or directory that you want to rename, then write the new name that you want it to be renamed into:
- We renamed the directory from "Public" to "Private" using the mv command.
That's it for mv command, Now you might wanna take a 5-minute break, and then join me back in this article.
How to Create and Delete Files and Directories using Terminal
Creating Files
For creating files in linux, there are a few different options available.
The options are:
touch command - used to create empty files
cat command with redirection operator. Don't worry about cat or redirection operators ( >, >> ), I will teach you both well enough.
Editors like Nano and Vi ( Vim ). This topic is not in the scope of this article. I'll link it here if I decide to write a blog on it. For now, you can refer to this (not sponsored) or you may find another resource on this yourself if you prefer:
Creating files with touch command:
- touch is used to create empty files, a file that has no content in it.
When we write touch fileName, an empty file named "fileName" gets created.
Now using "cat fileName" we print the content of the file in the terminal itself.
But here nothing gets printed in the terminal. That confirms that the file is empty.
Don't worry about this cat command if you don't understand it. We'll be discussing it next.
cat command and how it can be used to create files using redirection operators:
cat command
cat stands for concatenation.
Let me explain what it does in a bit simpler language;
It can be used to print outputs in the terminal and
cat command can be used for concatinatinating content into files ( in other words creating or updating files without ever leaving the home terminal itself with the help of redirection operators > and >> ).
cat to print output ( contents of a file )
Note: cat can be used to print output of operations etc. But we are discussing file content printing to the terminal only.
This one's very simple actually.
Just use cat followed by filename that you want to print:
- I went ahead and created this file with the content that you see in the screenshot. Read it to understand better.
That's it for printing the output with cat. Now let's learn to create files.
cat with redirection operators to create or update an existing file's content:
We used cat with a single redirection operator ( > ) followed by a file that we want to write the content in, here i.e., "fileName",
Then we typed the following:
Then we changed the line by pressing enter.
Then we used Ctrl + c to exit out of concatenation.
Now to verify use "cat fileName" to print the content of the file in the terminal:
We can see that the contents were concatenated into the file "fileName".
Note: If we use this "cat > fileName" on the same file "fileName" again then the file's content will be overwritten. That's why > operator is sometimes called an overwrite operator command.
Let's verify if it overwrites the content or not:
As we can see, it overwrites the previous content of the file. But there's another operator used for appending instead of overwriting the contents of the file, if we want to keep the previous content as well.
Append ( >> ) operator with the cat command:
This operator with a cat can be used to append newer content to an existing file without overwriting its previous content, appending the newer content from the next line.
- As we can see, the new contents were added from the next line of the file without overwriting the previous content.
That's all for now.
Creating Directories ( Folder )
mkdir ( make directory ) is the command used for creating directories.
Pretty simple syntax:
And if you want to create multiple directories then use the following:
NOTE: It is best that you don't use special characters or spaces in a file name or directory name as you did in Windows, except hyphens.
If you want you can do it but it will make your workflow slow as you'll have to escape those characters using \ while working in the terminal.
Honestly, that was a long one, you should consider taking a break. Come back after a while when you feel like it.
Next up, we'll learn to delete files and directories.
TAKE A BREAK FOR NOW.
Removing ( or deleting ) Files and Directories in terminal
Deleting or Removing Files - rm command
rm is the command used for deleting files.
The syntax is simple:
Deleting or Removing empty Directories - rmdir command
rmdir command is used to delete empty i.e., if a directory doesn't contain anything inside of it, then only this command will work.
The syntax is the same as deleting files:
Now if you tried deleting a directory that has contents in it then it will through an error:
Deleting or removing a directory with contents inside it using rm command:
To delete a directory with contents we need to use the old rm command with a special option "-r" which stands for recursive deletion.
So whatever the contents might be inside the directory, all of it will get deleted recursively until there's nothing inside the directory.
So let's try deleting this Private directory along with the contents that persist inside it i.e., a bla directory and a file named file.txt:
That's it.
Well done, give yourself a pat for having made it out to the end of this article. You deserve it.
Concluding:
Remember to practice these for at least one or two weeks so that these things make room in your head.
And if you want to advance in your Linux journey, I would suggest that you switch entirely to Linux. I did the same.
Also, keep an eye out for more blogs related to Linux if you linked the way I delivered it;
Consider giving a follow to my blog
And maybe dropping a like