Dototot
Dototot • Engaging Education

touch, rm, ls, and cp Files in the Bash Shell Petting Zoo

  • tutorial
  • By Jay Nielsen

For this Linux/Unix bash shell tutorial, we have an extra special field trip planned in the “Petting Zoo” directory. Seatbelts everyone!

cd "Petting Zoo"

Lets take a look around. Use the “ls” command to list the directory contents.

ls

zoo 01

Llamas, turkeys, and bison, oh my! I’d like to know a bit more about these files. The “-l” option shows us the file’s permissions, group owner, file size, and date last modified. Radical!

ls -l

zoo 02

Oh no! That poor llama hasn’t been modified in years! How sad, it must be lonely. Lets “touch” it.

touch llama

Touching a file updates the access and modification time of the file, so if we list the petting zoo directory contents again…

ls -l

zoo 03

We see the llama has been “touched” recently. Doesn’t that just make you feel all warm inside?

Lets touch everything!

touch *

Ouch! That hedgehog was unpleasant, but you know what would be amazing? A unicorn.

touch unicorn

It may not look like anything happened, but you have to believe!

ls

zoo 04

Check it out! The touch command created a new file when we requested a file that didn’t exist. Magical!

This unicorn seems lonely and empty, I think she needs a friend. Lets make one with the cp command.

cp unicorn unicorn_copy

They should get along nicely, seeing as they are identical twins.

ls -l

zoo 05

Not all files live out in the open, we can use the “-a” option with “ls” to show any hidden files.

ls -a

zoo 06

Oh em gee! It’s a “.chupacabra”. Wait, what is a chupacabra? The “file” command will shed some light on this.

file .chupacabra

zoo 07

It’s worse than I could possibly have imagined. The .chupacabra is a Windows executable, and it’s upsetting the free and open-range files! It’s time for some good old-fashioned human intervention.

rm .chupacabra

The .chupacabra is gone.

ls -a

zoo 08

But what’s this hidden “.den”?

file .den

zoo 09

It’s a directory! Lets see what’s inside.

ls -a .den

zoo 10

Holy blood-sucking cryptids! More “.chupacabras”. We need to recursively remove that “.den” directory, stat!

rm -r .den
ls -a

zoo 11

Peace has been restored to the “Petting Zoo” directory. Lets head home.

cd

zoo 12

Lets review what we learned on our field trip:

  • ls lists the contents of a directory.
  • touch updates the date accessed and date modified times of a file.
  • If touch is given a non-existent file, it will create one for you.
  • the file command describes the file type.
  • cp copies a file.
  • rm removes a file.
  • rm -r removes a directory and all of the files and directories inside of it.

Class dismissed!

Featured image credit: Sam Howzit


Dototot is a creative media company and think tank specializing in educational material. The contents of this website are licensed under a CC BY-NC-SA 4.0 License.