10 Linux commands to know to manage files


Want to start improving your terminal skills? The Linux command line has many flexible commands for managing files. Learn how to use the most common ones for your everyday tasks.

pwd

Find out where you are

When using the command line, you are always in a specific directory. Linux calls this the working directory. By default, your query – the text before your cursor on each line – will display the name of this directory. When you first open Terminal, this is usually your home directory.

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 to your working directory:

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

cd

Now that you know where you are, it’s time to learn how to navigate your file system cd commandmeans “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 providing a directory as the first argument. This directory can be an absolute path:

cd /usr/local/bin
The Linux cd command changes the current working directory as verified by pwd.

Or, relative to your current directory, it can be:

cd bin
cd ../docs
Linux cd command that displays samples with absolute and relative paths.

To quickly return to your home directory, you can call cd with no arguments:

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

etc

Check subfolders and subfolders

After navigating to a directory, you 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 alone to display the contents of the current directory:

ls
The linux fs command displays an array of files in two lines 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, including permissions, ownership, size, and modification date/time:

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

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

If you want a clearer indication, try the -F option. It adds “https://www.howtogeek.com/” to the end of directories and “@” to the end of links, making them easy to discover:

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

Note that you can add wildcards to list files that match the pattern. You can do it use wildcards with any command that accepts file arguments, not just ls.

touch

Create files out of thin air

This weird voice command lets you update file access and modification times. You might not think you’ll need that 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 creates a new file, then updates its modification time, verified with ls.

it’s mk

Create a new directory

To organize your file system, you may want to create folders to group related files. Remember that folders can contain subdirectories, etc. The mkdir command is simple: it creates a directory in the path of each argument you provide.

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

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

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

To create the full hierarchy at once, use the -p flag:

mkdir -p one/two/three
mkdir command to create a deep internal subdirectory using the -p flag.

cp

Clone the file

To create an additional copy of an existing fileUse the cp (copy) command. In its simplest form, specify the path to the existing file and the path to the new copy:

cp resume.pdf resume2.pdf

You can also use this command to move multiple files to a folder. 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 a copy of three files in a subdirectory.

rm / is rm

Be careful with this one

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

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

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

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

rm -f filename

For example, if you try to delete a file without write permission, you will be prompted for confirmation. The -f flag skips this step; it also hides the error message if the file does not exist.

The Linux rm command with the -f option forces the file to be deleted.

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

The Linux rm command refuses to remove a directory.

The -d option will let you do this like rmdir:

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

etc

Move or rename the file

At first, it may 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 ways to change the path of a file.

mv old-name new-name

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

The Linux mv command renames a file and directory and moves a file to a directory.

mv has another mode that works like cp’s second mode. If the last argument is a directory, mv moves all other named files, even directories, to that directory.

chmod

Control permissions

In Linux, each file has a set of permissions describes who can get it and what they can do with it. “Who” can be the owner of the file, anyone in the same group as the file, or anyone in general. This “what” can be reading, writing, or executing—managing the file as a program.

using chmod it’s a bit complicated because of the permissions syntax. The main usage is:

chmod  files

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

chmod a+x script.sh

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

ln

Mirror files without duplication

Links allow you to refer to files multiple locations on the file system without taking up too much extra space. A symbolic or soft link is the easiest to understand:

ln -s original link

The link command creates a link to the original file:

The Linux ln command creates a link confirming ls.

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


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

there is more important Linux commands you must learn to do everything from killing a process to retrieving a remote web page.



Source link

Leave a Reply

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