l2t

Working with file and directories

How to work with files and directories in linux?

If you work with linux operating system you should know about how to efficiently work with files and directories.

In this tutorial we will cover all commands that are related to either files or directories.

How to list files or directories?

To see all files and directories within a given directory followings are some of the useful commands:

# list the contents of the current directory
ls dir_name/

# list the content of the directory including hidden files
ls -la dir_name/

How to make, switch or remove directories?

To create new directories in linux operating system use following commands:

# create a new directory
mkdir dir_name

# create a dir and create parent dir
# if it does not exists
mkdir -p dir_name/child_dir

# to remove a directory
rmdir dir_name

# This command forcefully and recursively 
# deletes a directory along with its content.
rm -rf dir_name

# to change the directory
cd target_dir
cd /tmp

# go one directory back
cd ..

How to create a file in linux?

To perform following operations on a file:

  • create a new file
  • remove a file
  • check file contents
  • get first/last lines of the files

following commands are useful:

# create a new file
touch hello.txt

# remove a file
rm hello.txt

# remove a file forcefully
rm -f hello.txt

# check the content of the file
cat hello.txt

# get the first 10 lines from the file
head hello.txt

# get first n number of lines from the file
head -n hello.txt

# get last 10 lines from the file
tail hello.txt

# get last n number of lines from the file
tail -n hello.txt

How to determine the file type of a file

# check the file type
file file.txt

# output of the above command
file.txt: ASCII text

# to check file type only
file -b file.txt

# output of the above command
ASCII text

# check the mime type of the file
file -i file.txt

# output of the above command
file.txt: text/plain; charset=us-ascii

# checking mime type without filename
file -i -b file.txt

# output of the above command
text/plain; charset=us-ascii

# check the file type for compressed files
file -z bar.txt.gz

# output of the above command
bar.txt.gz: ASCII text (gzip compressed data, was "bar.txt", last modified: Wed Sep  7 19:31:23 2016, from Unix)

credit: https://shapeshed.com/unix-file/

How to copy files in linux?

To copy files from one to other location in linux operating system following commands are used:

# copy all html files from source to destination folder
cp -u source/*.html destination/

How to move files in linux?

To move files from one to other location in linux operating system following commands are used:

# copy all html files from source to destination folder
mv source/*.html destination/

# This command copies the content of directory source into directory destination.
cp -r source/ destination/

How to remove files in linux?

To remove files in linux operating system following commands are used:

# remove single text file
rm file.txt

# remove all text file
rm *.txt

# force remove all files along with folder
rm -rf destination/

Note: Linux does not have an undelete command so be careful if you delete files or folders.