10 Linux commands to know for managing files


Want to start leveling up your terminal skills? The Linux command line has a wealth of flexible commands for file management. Learn how to use the most common ones for your everyday tasks.

pwd

Find out where you are

When you’re using the command line, you’re always “in” a specific directory. Linux calls this the working directory. By default, your prompt—the text before your cursor on each line—will display the name of this directory. It’s usually your home directory when you first open a terminal.

The pwd command stands for “print working directory” and you can think of it like a “you are here” marker. When you run pwd, it tells you the full (absolute) path of your working directory:

The linux pwd command showing the current directory is "/usr/local/bin."

cd

Now you know where you are, it’s time to learn how to move around your filesystem with the cd command, which stands for “change directory.” Your working directory is important because all commands and files that use relative paths do so from that directory.

Run the cd command by supplying a directory as the first argument. This directory can be a complete absolute path:

cd /usr/local/bin
The linux cd command changes the current working directory, confirmed by pwd.

Or it can be relative to your current directory:

cd bin
cd ../docs
The Linux cd command showing examples with absolute and relative paths.

You can call cd without any arguments to quickly return to your home directory:

cd
The Linux cd command with no arguments changes directory to the user's home.

ls

Check subfolders and subdirectories

Once you’ve navigated to a directory, you’ll often want to know what files (including subdirectories) it contains. The ls command stands for “list” and, unlike the previous two commands, it has many options.

Use ls on its own to show the contents of the current directory:

ls
The linux fs command displaying a set of files in two rows and five columns.

You can list specific files or directories by passing them as arguments. Use the -l flag (long format) to display one file per line with full information including permissions, ownership, size, and modification date/time:

ls -l /boot
The ls command showing a list of files, one per line, each with metadata like permissions, owner, and modification date.

This format also displays the type of each file. If the first letter of the permission string is a “d”, it’s a directory. If it’s an “l”, it’s a link. If it’s a “-“, it’s a regular file.

If you want a more obvious indicator, try the -F option. It adds a “https://www.howtogeek.com/” to the end of directories and an “@” to the end of links, making them easier to spot:

ls -F /bin/*.grep
The Linux ls command with the -F option which adds a "*" at the end of executables and an "@" at the end of links.

Note that you can include wildcard characters to list files matching a pattern. You can use wildcards with any command that accepts file arguments, not just ls.

touch

Create files from thin air

This strange-sounding command lets you update a file’s access & modification times. You might not think you’d need this much, and you’re probably right, but touch has a secondary purpose: to create an empty file.

Run the command with any number of arguments, then touch will update existing files and create new ones:

touch foo bar
The touch command creating a new file, then updating its modification time, confirmed by an ls.

mkdir

Create a new directory

To organize your filesystem, you’ll want to create directories to group related files. Remember that directories can contain subdirectories, and so on. The mkdir command is simple: it creates a directory at the path of each argument you supply.

mkdir foo foo/bar hum
The Linux mkdir command creating three directories, including one subdirectory.

Note that each path can be relative or absolute. If you try to create multiple levels of directory at the same time, mkdir will complain:

The Linux mkdir command showing an error when trying to create a deep subdirectory.

Use the -p flag to create the full hierarchy in one go:

mkdir -p one/two/three
The mkdir command using the -p flag to create a deeply nested subdirectory.

cp

Clone a file

To create an additional copy of an existing file, use the cp (copy) command. In its simplest form, give the path of an existing file and a path for the new copy:

cp resume.pdf resume2.pdf

You can also use this command to copy several files to a directory. To do so, use as many file arguments as you like, followed by a single directory:

cp file1.md file2.md file3.md myfiles
The Linux cp command creating copies of three files in a subdirectory.

rm / rmdir

Be careful with this one

Eventually, you’ll find you’ve created too many files and you’ll want to get rid of some. Enter rm, short for “remove.”

The rm command takes one or more files as arguments and will attempt to delete each one:

rm resume *.bak 
The Linux rm command deleting a single file.

Use the -f option to “force” removal of a file. This attempts to delete a file, even if its permissions appear to restrict you from doing so:

rm -f filename

For example, without write permission, you’ll be prompted for confirmation if you try to delete a file. The -f flag skips this step; it also suppresses an error message if the file doesn’t exist.

The Linux rm command with the -f option forcing deletion of a file.

rm will also let you remove directories, but not by default:

The Linux rm command refusing to delete a directory.

The -d option will let you do so, as will rmdir:

The Linux rm command used with the -d option to remove a directory.

mv

Move or rename a file

Initially, it might seem like the mv command (short for “move”) has a dual-purpose: both renaming files and moving them from one directory to another. However, Linux considers both just ways of changing a file’s path.

mv old-name new-name

With two arguments, mv will rename—or move—the first to the second:

The Linux mv command renaming a file and a directory and moving a file into a directory.

mv has another mode that works a bit like cp’s second mode. If the final argument is a directory, mv will move all other named files into that directory, even those that are directories.

chmod

Take control of permissions

On Linux, each file has a set of permissions describing who can access it and what they can do with it. The “who” can be the owner of the file, anyone in the same group as the file, or anyone at all. That “what” can be reading, writing, or executing—running the file as a program.

Use of chmod is a bit complicated because of its permissions syntax. Basic use is like this:

chmod <permissions> files

An example of permissions is “go+r” which makes (+) a file readable (r) to users in its group (g) and other users (o). Another typical use is to make a file executable:

chmod a+x script.sh

This lets you run a script on the command line by typing its path.

ln

Mirror files without duplicating them

Links let you reference files in more than one place on the file system, without taking up much extra space. The symbolic—or soft—link is the easiest to understand:

ln -s original link

The link command creates a link that references an original file:

The linux ln command creates a link which ls confirms.

Without the -s option, you’ll create a hard link, which acts more like a second copy of the original file. They are a bit harder to understand and, therefore, are less commonly used.


Don’t stop here, there’s always more to learn

There are many more important Linux commands you should learn to do anything from killing a process to fetching a remote web page.



Source link

Leave a Reply

Subscribe to Our Newsletter

Get our latest articles delivered straight to your inbox. No spam, we promise.

Recent Reviews


Whoop MG on arm

The Whoop is one of the devices that Google’s rumored screenless health tracker would compete with.

Nina Raemont/ZDNET

Follow ZDNET: Add us as a preferred source on Google.


ZDNET’s key takeaways 

  • Google is poised to unveil a Whoop dupe soon. 
  • Steph Curry teased a screenless health band on his Instagram. 
  • Here’s what I’d like to see from a Google fitness band. 

Could Google’s latest fitness tracker return to its original, screenless Fitbit form? All signs say yes. Google has teased a screenless, Whoop-adjacent health tracker with the help of basketball star Steph Curry. A recent Instagram post from Curry shows him wearing a screenless, fabric band around his wrist, and the accompanying caption promotes “a new relationship with your health.” 

There are scant confirmed details on this next device, but rumors suggest the band will be called “Fitbit Air.” 

Also: I replaced my Whoop with a rival fitness band that has no monthly fees – and it’s nearly as good

Why a screenless fitness band? And why now? Google’s new device could be taking interest away from popular fitness brand Whoop. Whoop’s fitness band is on the more luxurious end of the health wearables spectrum. The company offers three subscription tiers, starting at $199, $239, and $359 annually. Google’s device, on the other hand, is rumored to be more affordable with the option to upgrade to Fitbit Premium. 

Google has the opportunity to make an accessibly priced fitness band with the rumored Fitbit Air and breathe new life into its older Fitbit product lineup, which hasn’t been updated in years. 

What I’m expecting 

Here’s what I expect to see and what I hope Google prioritizes in this new health tracker.

Given Fitbit’s bare-bones approach to fitness tracking, I assume Google will emphasize an affordable, accessible fitness band with the Fitbit Air. Most Fitbit products cost between $130 and $230, so I’m expecting this band to be on the lower end of that price range. I’d also expect Fitbit to give users a free trial of Fitbit Premium. 

Also: T-Mobile is practically giving away the Apple Watch Series 11 – here’s how to get one

A long, long, long battery life 

A smartwatch with a bright screen and integrations with an accompanying smartphone consumes a lot of power. That’s why some of the best smartwatches on the market have a middling battery life of one to two days, tops. 

A fitness band, on the other hand, is screenless. That makes the battery potential on this Fitbit Air double — or even triple — that of Google’s smartwatches.

Also: I use this 30-second routine to fix sluggish Samsung smartwatches – and it works every time

The Fitbit Inspire 3 has around 10 days of battery life — with a watch display. I hope the screenless Fitbit Air has at least 10 days of battery life, plus some change. Two weeks of battery life would be splendid. 

In addition to usage time, I also hope that a screenless fitness tracker addresses some of the issues Fitbit Inspire users have complained about. Many Inspire users report that the device’s screen died after a year of use. They could still access data through the app, but the screen was dysfunctional. Despite being a more affordable Google health tracker, the Fitbit Air should last users for a few years without any hardware issues — or at least I hope it does. 

Fitbit’s classically accurate heart rate measurements 

As Google’s Performance Advisor and the athlete teasing Google’s next device, Steph Curry is sending the message that this new device, one that offers wearers “a new relationship with your health,” will be built for athletes and exercise enthusiasts. I hope this device homes in on accurate heart rate measurements and advanced sensing, as other Fitbit devices do. 

Also: I walked 3,000 steps with my Apple Watch, Google Pixel, and Oura Ring – this tracker was most accurate

Like Whoop, I hope the insights the Fitbit Air provides are performance- and recovery-driven. Whoop grew in popularity for exactly this reason. Not only do Whoop users get their sleep and recovery score, but they also see, through graphs and health data illustrations, how their daily exercise exertion, strain, and sleep interact with and inform each other. 

I’m assuming that Fitbit Premium, with its AI-powered health coach and revamped app design, may do a lot of the heavy lifting for sleep and recovery insights with this new product. 

Also: Are AI health coach subscriptions a scam? My verdict after testing Fitbit’s for a month

But I also hope Google adds a few features on the app’s home screen that specifically target athletic strain and recovery, beyond the steps, sleep, readiness, and weekly exercise percentage already available on the Fitbit app’s main screen. 

Lots of customizable, distinct bands 

I hope the Fitbit Air is cheap — and the accompanying bands are even cheaper. If the rumors of affordability are true, then I’d hope Fitbit sells bands that can be worn with the device that match users’ styles and color preferences at a similarly affordable and accessible price point. Curry wears a gray-orange band in his teaser. I hope the colorways for this device are bold, patterned, and easily distinguishable from rival fitness bands. 





Source link