Linux Command Line Basics: Copy, Move, and Remove files and Directories

Command Line, Linux

I have a love of mystery and the command line is a mystery I look forward to understand. This article is a basic tutorial of how I learned to copy, move, and remove files and directories using the Linux command line.

The command line is the interface where we (people) can make the magic happen with computers. Basically we can do some powerful sh*t with fewer resources. For example we can search for files quickly, we can compress files easily, and we can secure communication with remote users. At least that is what I have been told and read online. The possibility of being able to make a computer do more cool sh*t is what inspired me to teach myself how the CLI (command line interface) works.

Basic Commands

First things first: you have to know where the reference manuals are for the commands. If you don’t, you could be functioning blind and just following others commands.

man

This command will show the reference manuals. Picture this command as the subconscous brain. It’ll show you what, and how these commands can be used.

  • man: short for manual
  • man + command will show you reference manual

“learn by doing”

  • open the terminal
  • enter man pwd on keyboard
  • what did you discover?

pwd

pwd command shows the directory you are currently in. Imagine this command as being your eyes for your present location.

  • print working directory
  • shows the current directory

“learn by doing”

  • open the terminal
  • enter “pwd” on keyboard
  • what did you discover?

clear

The clear command wipes the screen clear. This command is the broom you can use to give you a clean screen to work with.

  • clears the terminal

“learn by doing”.

  • enter “clear” on keyboard
  • what did you discover?

mkdir

The mkdir command is how you create new folders to work with. Directories are the places where the memories of you computer can be stored.

  • mkdir creates new directory

“learn by doing”.

  • open the terminal
  • enter “pwd” on keyboard
  • enter “mkdir newdir/” on keyboard
  • enter “ls” on keyboard
  • what did you discover?

ls

When ever you want to see the files listed in the current working director type the ls command. This command is the equivalent of you checking a list of ingredients for the wonderful food you make.

  • list files
  • shows the files in working directory

“learn by doing”.

  • open the terminal
  • enter “ls” on keyboard
  • what did you discover?

ls -a

If you want a deeper view of the ingredients of the files you are working with enter this flag at the end of the ls command. This allows for a more refine listing of the files viewed.

  • detail listing of all files in directory
  • notice the .bash files?
  • these are configuration files
  • not normally viewed with ls only

“learn by doing”.

  • open the terminal
  • enter “ls -a” on keyboard
  • what did you discover?

rmdir

We just created a new directory in the previous lbd. Now lets erase the directory from our memory.

  • rdmir removes empty directories only

“learn by doing”.

  • open the terminal
  • enter “rmdir newdir/” on keyboard
  • what did you discover?

cd

The cd command is how you change the directories you want to explore. Think about this command as if it was the remote control for your terminal. It allows you to surf the channels.

  • change directory

“learn by doing”.

  • open the terminal
  • enter “ls” on keyboard
  • enter “cd Documents” on keyboard
  • what did you discover?

“..”

Shortcuts are really helpful. I know my fingers get tired retyping certain keys and I love work arounds. Enter the “..”. This command allows you to hop up the tree quickly.

  • “..” moves up one level on the directory tree

“learn by doing”.

  • open the terminal
  • enter “ls” on keyboard
  • enter “cd Documents” on keyboard
  • enter “cd ..” on keyboard
  • what did you discover?

tab key

Since we are discussing shortcuts we can’t forget autocomplete. The tab button has the honor of thinking what the rest of the words will be after the first few characters have been entered.

  • tab is the autocomplete button

“learn by doing”.

  • open the terminal
  • enter “ls” on keyboard
  • enter “cd Doc + tab” on keyboard
  • what did you discover?

create files with vi editor

The vi editor allows you to edit and create files in the terminal. The powerful vi is a cool little program. My fingers tend to get mixed up dealing with how the commands are entered when using this editor. This will tend to correct itself after the muscle memory kicks in as you continue using it.

  • vi newfile

“learn by doing”.

  • open the terminal
  • enter “vi newfile” on keyboard
  • enter “i” on keyboard: input mode
  • enter “this is a new file” on keyboard
  • enter “:wq” on keyboard to save and escape vi editor
  • enter “ls” on keyboard
  • what did you discover?

mv

We’ve covered a bit of ground navigating through the command line interface. Now let’s create some files and directories using the vi editor.

  • mv: moves files/directories
  • mv: renames files/directories

“learn by doing”.

  • open the terminal
  • enter “mv newfile newfile1” on keyboard
  • what did you discover?

tail

The language for the command line is uber intuitive sometimes. Notice the commands are either abbreviations or one word concept statements for what the command might do. Now imagine what the tail command outputs?

  • shows the last part of files

“learn by doing”.

  • open the terminal
  • enter “tail newfile” on keyboard
  • what did you discover?

head

The file we created is like a body. It has abstract versions of tails and heads. Since we know what the tail command does, just imagine what the head command will show us!

  • shows the first part of files

“learn by doing”.

  • open the terminal
  • enter “head newfile” on keyboard
  • what did you discover?

cat

This is such a chill word. The word catenate literally means chain in Latin. This command joins all the files together when summoned by the command line.

  • links together (concatenate) files

“learn by doing”.

  • open the terminal
  • enter “cat newfile” on keyboard
  • what did you discover?

cp

Lets learn how to clone files and directories with the copy command.

  • copies file and directories

“learn by doing”.

  • open the terminal
  • enter &quotcp newfile newfile1;” on keyboard
  • enter “ls” on keyboard
  • what did you discover?
  • open the terminal
  • enter &quotcp newdir/ newdir1/;” on keyboard
  • enter “ls” on keyboard
  • what did you discover?

rm

Well it is time to learn how to clean up the directories and files a bit. You don’t want to create a sh*t load of directories and files without knowing the tools of how to remove them.

  • removes files and directories

“learn by doing”.

  • open the terminal
  • enter “ls” on keyboard
  • enter “rm newfile1” on keyboard
  • enter “ls” on keyboard
  • what did you discover?
  • open the terminal
  • enter “ls” on keyboard
  • enter “rm -r newdir/” on keyboard
  • enter “ls” on keyboard
  • what did you discover?

Conclusion

The command line is a fun place to play in when you want to control what and how your computer functions. The skills you develop take time and patience. Crush it!

Leave a Reply