10 Linux commands you should know to manage files


Do you want to start improving your terminal skills? The Linux command line has a large number of flexible commands for file management. Learn to use the most common ones for your daily tasks.

disabled person

Find out where you are

When you use the command line, you are always “in” a specific directory. Linux calls this the working directory. By default, your message (the text before the cursor on each line) will display the name of this directory. This is generally your home directory when you first open a terminal.

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

The Linux pwd command that shows the current directory is "/usr/local/bin."

CD

Now that you know where you are, it’s time to learn how to move around your file system with the cd commandwhich means “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 providing 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 the directory at the user's home.

is

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 means “list” and, unlike the previous two commands, it has many options.

Use ls only to display the contents of the current directory:

ls
The linux fs command displays 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 (long format) flag to display one file per line with complete information, including permissions, ownership, size, and modification date/time:

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

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

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

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

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

tap

Create files from scratch

This strange sounding command allows you to update the access and modification times of a file. You may not think you would need that much and you are probably right, but tap has a secondary purpose: to create an empty file.

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

touch foo bar
The touch command creates a new file and then updates its modification time, confirmed by an ls.

mkdir

Create a new directory

To organize your file system, you’ll want to create directories to group related files. Remember that directories can contain subdirectories, etc. 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 creates three directories, including a subdirectory.

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

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

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

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

CP

Clone a file

To create an additional copy of an existing fileuse the cp (copy) command. In its simplest form, provide 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 multiple files to a directory. To do this, 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 creates copies of three files in a subdirectory.

rm/rmdir

Be careful with this one

Over time, you will find that you have created too many files and you will want to get rid of some. Enter rm, short for “delete.”

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

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

Use the -f option to “force” delete a file. This attempts to delete a file, even if its permissions seem to prevent it from doing so:

rm -f filename

For example, without write permission, you will 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 does not exist.

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

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

Linux's rm command refuses to delete a directory.

The -d option will allow you to do this, as will rmdir:

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

mv

Move or rename a file

Initially, it may seem that the mv (short for “move”) command has a dual purpose: to rename files and to move them from one directory to another. However, Linux considers both ways to change a file path.

mv old-name new-name

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

The Linux mv command renames a file and a directory and moves a file to 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 to 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. That “what” can be read, write, or execute: execute the file as a program.

use of chmod It’s a bit complicated because of its permissions syntax. The basic usage is like this:

chmod  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 convert a file into an executable:

chmod a+x script.sh

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

in

Duplicate files without duplicating them

Links allow you to reference files. in more than one place in the file system, without taking up much additional 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 that confirms them.

Without the -s option, you will create a hard link, which acts more like a second copy of the original file. They are a little more difficult to understand and are therefore used less frequently.


Don’t stop here, there is always more to learn.

There is many more important linux commands you should learn how to do anything from ending a process to retrieving a remote web page.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *