Linux folders with spaces

How to reference filename with spaces in Linux

Various reasons keep a new Linux user away from the command line, one of them handling files in the terminal. Files and directories in Linux can have different names, and some names can have “spaces” in them. So what’s the big deal? The issue is, the terminal reads the “space” differently; for instance, if your directory has the name “new folder,” the terminal will assume two different directories, i.e., “new” and “folder.” Accessing such files in the terminal can become really frustrating. Luckily, there are various methods to deal with the files/folders with spaces in their names.

This guide is focusing on how to reference filename with spaces in Linux with different approaches. So let’s begin:

How to access files/directories in the terminal with spaces in the name:

In this section, we will be creating a file first with a “space” in its name; then, we will learn what errors we face while referring to it and then how to access it correctly in the terminal:

How to create a file and directory with space in its name:

Creating a file in Linux with “space” in its name is straightforward, open terminal, and run the command mentioned below:

The file can either be created by using apostrophes or quotation marks. The procedure of creating a directory is quite similar:

You can verify it by using the “ls” command in the terminal.

How to read a file with space in its name:

Before we learn the correct way to read a file with “space” in its name, let’s identify the error it can give. So when you try to read the above-created file (my file), you will get an error:

Now, let’s see what happens when you try to write something to the file:

As it can be seen that the above command, instead of writing the “my file,” creating a new file by the name of “my” and saving text to it. So, how to access such a file? Well, there are two approaches:

  • Using escape character, i.e., “\ ”
  • Using apostrophes or quotation marks

So first of all, let’s insert some text into the above-created file using ;“\ ”:

Источник

Linux: Create directory or folder using mkdir command

In this article we will discuss how to create a single or multiple directories in Linux using mkdir command. Then we will see various examples like, creating folders with space in name or creating folders with different permissions or creating nested directories recursively in single command.

Читайте также:  Esd подписка windows что это

Table of Contents

mkdir command – Introduction

Linux provides a command mkdir to create single or multiple directories,

  • -m or –mode: Set the mode / permissions like chmod.
  • -p or –parents: Make parent directories if required and show no error in case directory exists.
  • -v or –verbose: Print a message for each of the created directory.

mkdir command creates the folder if it does not exists. If the folder already exists then based on the option provided, it will decide whether to show error or not. Let’s see some examples,

Create a single directory in Linux with mkdir command

To create a single directory in Linux, just provide the folder name along with mkdir command. For example,

It created a directory with name ‘project’. Let’s verify this,

Create multiple directories in Linux with mkdir command

To create multiple directories in a single command provide all the names of directories to mkdir command, separated by space. For example,

It created three directories ‘src’, ‘cfg’ and ‘lib’. Let’s verify this,

We can also use the verbose option i.e. -v while creating multiple directories. It will display the message after creating each directory. For example,

What if we try to create a directory that already exist ? Will it show error or overwrite it ? Let’s know about this.

Linux: Create directory if not exists

If we use the mkdir command to create a directory that already exist, then by default mkdir will give error. For example,

As ‘src’ directory already exists, therefore when we tried to create a new directory with same name using mkdir command (without any option) then it raised the error.

If you want a behavior that it should create a directory if it does not exist and in case directory already exists then just skip, without any creation.
Then we can do that using -p option. For example,

As ‘src’ directory already exists, therefore mkdir will not create new directory with same name. But as -p option was provided, therefore it didn’t gave an error. It just skipped the folder creation. So, when we use -p option flag with mkdir command then it will only create the directory if does not exist.

Linux: Create directory and subdirectories in one command – Recursively

If you create a folder and subfolders inside it in a single command then you need to provide -p option. Where -p stands for parents, it means if a complete folder path is given and any parent folder in the path is missing, then it will create that too. Let’s understand with some examples,

Let’s try to create a folder ‘source’ and inside the folder ‘source’ create a folder ‘code’ and inside that an another folder ‘test’. Nested folder structure should be like, ‘source/code/test’. If we try to create this nested folder structure with mkdir command without any option then it will give error,

By default mkdir command, can not create missing parent folders. To automatically create missing parent folders, use the -p option with mkdir. For example,

It created the completed nested folder structure recursively. Let’s verify this,

So, to create folder and subfolders in a single command in linux, use -p option with mkdir command.

Linux: Create directory with space in name

In Linux, creating a directory with space in name is little tricky, because if we directly provide a name with space to the mkdir command then it will create two directories instead of one. Let’s understand with an example,
Suppose we want to create a directory “test dir” using mkdir command. Let’s pass this name with space to mkdir command directly,

Читайте также:  Сброс пароля windows recovery

It will create two folders ‘dir’ & ‘test’. Let’s verify this,

This is not what we wanted. So, to create a folder with space in name we have two techniques,

Escape the spaces in name and pass to mkdir command

Escape the spaces in the name while passing it to the mkdir command. For example,

It created a folder with space in name. Let’s verify this,

Escape the spaces in name and pass to mkdir command

Encapsulate the folder name with spaces in quotes and pass it to mkdir command,

It created a folder with space in name. Let’s verify this,

Linux: Create directory with permissions

To create a directory with permissions we can use the -m option or -mode option. The syntax of permissions that we need to provide with -m option is similar to chmod. For example,

It will create a directory test_dir with permissions “rwxrwxrwx”. Let’s verify this,

Linux: Create folder with date

To create a directory with mkdir command we need to use the command substitutions. For example,

Create a directory with today’s date

It created a folder with today’s date. Let’s verify this,

Here the output of command $(date +%Y-%m-%d) is today’s date in YYYY-MM-DD format and we inserted that string to mkdir command, which in turns creates the folder with today’s date in format YYY-MM-DD.

Create a directory with today’s date and time

Here the output of date command was passed to mkdir command and it in turn created a folder with today’s timestamp.

Summary

We learned how to create directories in Linux using mkdir command and we also explored all the options of mkdir command like created nested directories & setting permissions etc.

Источник

How to Read a Filename with Spaces in Linux

It’s not very common in Linux to handle filename with spaces but sometimes files copied or mounted from windows would end up with spaces.

While it is not recommended to have file names with spaces, let discuss how to manage filename with spaces in a Linux system.

We will cover how to create, read and copy a file which has spaces in their filename.

1) Creating file names with spaces

To create files with spaces in file names, run the command as shown

For example, to create a file called ‘linoxide docs‘ use the syntax below

Output

If you want to view such a file with space in the file name, use the same principle of enclosing the file names inside the quotation marks.

2) Read a File with spaces in filename

You can use ‘cat’ command or open the document using your preferred text editor such as vim, nano or gedit.

Alternatively, you can use the syntax below

Let’s add some text to the ‘linoxide docs’ file

To view the file execute the command below

Output

3) Creating directory names with spaces

To create directory names with space in between use the syntax below

Please note the space after the backslash

For example, to create a directory called ‘linoxide files‘ run

Output

4) Navigating to a directory with spaces in the directory name

To navigate to a directory with spaces in its directory name, use the syntax below

To navigate to the directory ‘linoxide files’ execute the command below

5) Copying a directory with spaces in the directory name

To copy a directory with spaces in its directory name to a different location use the syntax below

Читайте также:  Ubuntu что такое linux headers

For example to copy ‘linoxide files’ to /home/james path execute

Hope this article explained well on how to manage filename with spaces. Thanks for taking the time to read this article and please leave your comments.

Источник

How to Find Out Top Directories and Files (Disk Space) in Linux

As a Linux administrator, you must periodically check which files and folders are consuming more disk space. It is very necessary to find unnecessary junk and free up them from your hard disk.

This brief tutorial describes how to find the largest files and folders in the Linux file system using du (disk usage) and find command. If you want to learn more about these two commands, then head over to the following articles.

How to Find Biggest Files and Directories in Linux

Run the following command to find out top biggest directories under /home partition.

Find Largest Directories in Linux

The above command displays the biggest 5 directories of my /home partition.

Find Largest Directories in Linux

If you want to display the biggest directories in the current working directory, run:

Find Biggest Directories Only

Let us break down the command and see what says each parameter.

  1. du command: Estimate file space usage.
  2. a : Displays all files and folders.
  3. sort command : Sort lines of text files.
  4. -n : Compare according to string numerical value.
  5. -r : Reverse the result of comparisons.
  6. head : Output the first part of files.
  7. -n : Print the first ‘n’ lines. (In our case, We displayed the first 5 lines).

Some of you would like to display the above result in human-readable format. i.e you might want to display the largest files in KB, MB, or GB.

Find Top Directories Sizes in Linux

The above command will show the top directories, which are eating up more disk space. If you feel that some directories are not important, you can simply delete a few sub-directories or delete the entire folder to free up some space.

To display the largest folders/files including the sub-directories, run:

Find Largest Folder and Subdirectories

Find out the meaning of each option using in above command:

  1. du command: Estimate file space usage.
  2. -h : Print sizes in human-readable format (e.g., 10MB).
  3. -S : Do not include the size of subdirectories.
  4. -s : Display only a total for each argument.
  5. sort command : sort lines of text files.
  6. -r : Reverse the result of comparisons.
  7. -h : Compare human readable numbers (e.g., 2K, 1G).
  8. head : Output the first part of files.

Find Out Top File Sizes Only

If you want to display the biggest file sizes only, then run the following command:

Find Top File Sizes in Linux

To find the largest files in a particular location, just include the path beside the find command:

Find Top File Size in Specific Location

The above command will display the largest file from /home/tecmint/Downloads directory.

That’s all for now. Finding the biggest files and folders is no big deal. Even a novice administrator can easily find them. If you find this tutorial useful, please share it on your social networks and support TecMint.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

Оцените статью