DEV Community

Quynh Nguyen
Quynh Nguyen

Posted on

Linux basics - Working with files and directories

In this article, I will list commands that help interact with files and directories on a Linux system, along with the most common use cases. I write them down here as a short note. This article may be updated over time.

Print working directory

pwd
Enter fullscreen mode Exit fullscreen mode

Change working directory

  • Use an absolute path.
cd /var/log
Enter fullscreen mode Exit fullscreen mode
  • Use a relative path.
cd apt
Enter fullscreen mode Exit fullscreen mode

I'm already in /var/log directory, it goes inside /var/log/apt

  • Go to the user home directory.
cd

cd ~
Enter fullscreen mode Exit fullscreen mode
  • Go back the previous directory.
cd -
Enter fullscreen mode Exit fullscreen mode

List items

  • List items in the current directory.
ls
Enter fullscreen mode Exit fullscreen mode
  • List items in a specific directory.
ls /etc/
Enter fullscreen mode Exit fullscreen mode
  • Add more options to the ls command.
    • -l: Use a long listing format.
    • -a: Display all items including one starts with dot (.)
    • -h: Display sizes in 1K, 2M, etc, which is easy to read.
    • --group-directories-first: Display directories first, then files.
ls -lah --group-directories-first /etc/
Enter fullscreen mode Exit fullscreen mode

Create an empty file

If hello.txt doesn't exist, it'll be created. Otherwise, it's modified time will be changed.

touch hello.txt
Enter fullscreen mode Exit fullscreen mode

Copy

  • Copy 1.txt to one.txt in the current directory.
cp 1.txt one.txt
Enter fullscreen mode Exit fullscreen mode
  • Copy one.txt to the /tmp directory.
cp one.txt /tmp
Enter fullscreen mode Exit fullscreen mode
  • Copy /var/log directory to /tmp directory.
cp -r /var/log /tmp/
Enter fullscreen mode Exit fullscreen mode
  • Copy /var/log to /tmp, and change it's name to cp-log.
cp -r /var/log /tmp/cp-log
Enter fullscreen mode Exit fullscreen mode

Move or rename

  • Rename hello.txt to hello-world.txt. If hello-world.txt exists, it'll be override.
mv hello.txt hello-world.txt
Enter fullscreen mode Exit fullscreen mode
  • Move a directory or file to new location.
mv hello-world.txt /tmp/
mv example /tmp/
Enter fullscreen mode Exit fullscreen mode

Make directories

  • Use -p option to create nested directories or keep silent if directories already exist.
mkdir example
mkdir -p example/foo/bar/baz 
Enter fullscreen mode Exit fullscreen mode

Remove directories or files

  • Add -f option to force to remove without confirmation or raising errors if they does not exist.
rm hello.txt

rm -r greetings/
Enter fullscreen mode Exit fullscreen mode

rmdir can only remove an empty directory, so I've never used it.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more