Skip to content

Working with files and directories

In this section we will focus on files and directories.

In Unix, files and directories are similar

A computer file is a named collection of data that is stored on a computer's hard drive or other storage device. Files can contain text, images, audio, video, and other types of data. Files are typically opened and closed using a file manager or other software application.

A directory is a named collection of files and other directories. Directories are used to organize files and make them easier to find. Directories are typically opened and closed using a file manager or other software application.

Similarities and differences between files and Directories

Similarities

  • Both files and directories are stored on the same physical disk.
  • Both files and directories have names.
  • Both files and directories can be owned by users and groups.
  • Both files and directories can have permissions set on them.
  • Both files and directories can be accessed using the same commands.

Differences

  • A file is a collection of data, while a directory is a collection of files.
  • A file cannot contain other files, while a directory can contain other files and directories.
  • A file can be accessed directly by its name, while a directory can only be accessed by navigating to it through the file system hierarchy.

List of commands for working with Files and Directories

The following table shows some of the main Unix commands for working with files and directories:

Command Description
ls List the contents of a directory
cd Change the current directory
mkdir Create a new directory
rmdir Remove an empty directory
touch Create a new empty file
cat Display the contents of a file
cp Copy a file
mv Move a file
rm Remove a file ⚠️
more Display the contents of a file one screen at a time
less Display the contents of a file one screen at a time and allow scrolling
head Output the first part of file
tail Output the last part of file
grep Search for a pattern in a file
sort Sort the lines in a file
uniq Remove duplicate lines from a file
find Find files that match a certain criteria
chmod Change the permissions on a file
chown Change the ownership of a file
ln Create a link to a file
tar Create a compressed archive of a file or directory
gzip Compress or decompress a file
bzip2 Compress or decompress a file

References


Created: 05/13/2023: Updated: 05/15/2023

Carlos Lizárraga