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
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
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
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
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
Not all files live out in the open, we can use the “-a” option with “ls” to show any hidden files.
ls -a
Oh em gee! It’s a “.chupacabra”. Wait, what is a chupacabra? The “file” command will shed some light on this.
file .chupacabra
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
But what’s this hidden “.den”?
file .den
It’s a directory! Lets see what’s inside.
ls -a .den
Holy blood-sucking cryptids! More “.chupacabras”. We need to recursively remove that “.den” directory, stat!
rm -r .den
ls -a
Peace has been restored to the “Petting Zoo” directory. Lets head home.
cd
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