- How to Read a Filename with Spaces in Linux
- 1) Creating file names with spaces
- 2) Read a File with spaces in filename
- 3) Creating directory names with spaces
- 4) Navigating to a directory with spaces in the directory name
- 5) Copying a directory with spaces in the directory name
- Linux Check Disk Space Command To View System Disk Usage
- Linux commands to check disk space using:
- Linux check disk space with df command
- See information about specific filesystem
- Understanding df command output
- Express df output in human readable form
- Display output using inode usage instead of block usage
- Find out the type of each file system displayed
- Limit listing to file systems of given type
- Exclude given file system type
- Show all file system
- Getting more help about the df command
- Linux check disk space with the du command
- See du output in human readable format
- Finding information about any directory trees or files
- How do I summarize disk usage for given directory name?
- Putting it all together
- Dealing with btrfs file system
- Examples
- Conclusion
- Dealing With Spaces in File Names in Command-line (GNU/Linux)
- If a path contains two or more file names with spaces …
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
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.
Источник
Linux Check Disk Space Command To View System Disk Usage
Linux commands to check disk space using:
- df command – Shows the amount of disk space used and available on Linux file systems.
- du command – Display the amount of disk space used by the specified files and for each subdirectory.
- btrfs fi df /device/ – Show disk space usage information for a btrfs based mount point/file system.
Linux check disk space with df command
- Open the terminal and type the following command to check disk space.
- The basic syntax for df is:
df [options] [devices]
Type: - df
- df -H
Fig.01: df command in action
See information about specific filesystem
You can give a device or mount point as an argument, and df report data only for the filesystem physically residing on that device. For example, the following command provides information only for the partition /dev/sda:
$ df /dev/sda
$ df -h /dev/sdc1
$ df /data/
Sample outputs:
Understanding df command output
The valid fields are as follows:
Display name | Valid field name (for —output option) | Description |
---|---|---|
Filesystem | source | The source of the mount point, usually a device. |
1K-blocks | size | Total number of blocks. |
Used | used | Number of used blocks. |
Available | avail | Number of available blocks. |
Use% | pcent | Percentage of USED divided by SIZE. |
Mounted on | target | The mount point. |
You can pass the output format defined by ‘valid field name’ as follows:
$ df —output=field1,field2.
$ df —output=source,used,avail /data/
Sample outputs:
You can print all available fields, enter:
$ df —o
Sample outputs:
Express df output in human readable form
Pass the -h option to see output in human readable format. You will device size in gigabytes or terabytes or megabytes:
$ df -h ### Human format
$ df -m ### Show output size in one-megabyte
$ df -k ### Show output size in one-kilobyte blocks (default)
Display output using inode usage instead of block usage
An inode is a data structure on a Linux file system that stores all information about file. To list inode information, enter:
$ df -i
$ df -i -h
Sample outputs:
Find out the type of each file system displayed
Pass the -T option to display the type of each filesystems listed such as ext4, btrfs, ext2, nfs4, fuse, cgroup, cputset, and more:
$ df -T
$ df -T -h
$ df -T -h /data/
Sample outputs:
Limit listing to file systems of given type
The syntax is:
$ df -t ext3 #Only see ext3 file system
$ df -t ext4 #Only see ext4 file system
$ df -t btrfs #Only see btrfs file system
Exclude given file system type
To list all but exclude ext2 filesystem pass the -x TYPE option, enter:
$ df -x ext2
Show all file system
Pass the -a or —all option to the df command to include in its output filesystems that have a size of zero blocks, run:
$ df -a
These file systems omitted by default.
Getting more help about the df command
Pass the —help option see a brief help message:
$ df —help
Or read its man page by typing the following command:
$ man df
Linux check disk space with the du command
The NA command is very useful to track down disk space hogs. It is useful to find out the names of directories and files that consume large amounts of space on a disk. The basic syntax is:
du
du /path/do/dir
du [options] [directories and/or files]
To see the names and space consumption of each of the directories including all subdirectories in the directory tree, enter:
$ du
Sample outputs:
The first column is expressed in kilobytes (file size) and the second column is the filename or directory name.
See du output in human readable format
Pass the -h option to display size in K (kilobytes), M (megabytes), G (gigabytes) instead of the default kilobytes:
$ du -h
Sample outputs:
Finding information about any directory trees or files
To find out /etc/ directory space usage, enter:
# du /etc/
# du -h /etc/
The following will report the sizes of the thee files named hdparm, iptunnel and ifconfig that are located in the /sbin directory:
$ du /sbin/hdparm /sbin/iptunnel /sbin/ifconfig
$ du -h /sbin/hdparm /sbin/iptunnel /sbin/ifconfig
Sample outputs:
How do I summarize disk usage for given directory name?
Pass the -s option to the du command. In this example, ask du command to report only the total disk space occupied by a directory tree and to suppress subdirectories:
# du -s /etc/
# du -sh /etc/
Sample outputs:
Pass the -a (all) option to see all files, not just directories:
# du -a /etc/
# du -a -h /etc/
Sample outputs:
You can also use star ( * ) wildcard, which will match any character. For example, to see the size of each png file in the current directory, enter:
$ du -ch *.png
The -c option tells du to display grand total.
Putting it all together
To list top 10 directories eating disk space in /etc/, enter:
# du -a /etc/ | sort -n -r | head -n 10
Sample outputs:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
For more information on the du command, type:
$ man du
$ du —help
Dealing with btrfs file system
For btrfs filesystem use the btrfs fi df command to see space usage information for a mount point. The syntax is:
Examples
# btrfs fi df /data/
# btrfs fi df -h /data/
Sample outputs:
To see raw numbers in bytes, run:
# btrfs fi df -b /data/
OR
# btrfs fi df -k /data/ ### show sizes in KiB ##
# btrfs fi df -m /data/ ### show sizes in MiB ##
# btrfs fi df -g /data/ ### show sizes in GiB ##
# btrfs fi df -t /data/ ### show sizes in TiB ##
Conclusion
Here is quick summary for Linux check disk space commands. Use the du command when you need to estimate file space usage. To report Linux file system disk space usage use the df command. The btrfs df command must be used when using btrfs file system. Fore more info see GNU coreutils page here.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Nice article. What about “ncdu”
For graphical overview… 🙂
How do i format a 39TB drive with ext4?
web-a1
Источник
Dealing With Spaces in File Names in Command-line (GNU/Linux)
Most people that use the GNU/Linux operating system do not like to deal with the command-line at all, though having a basic understanding of it (such as memorizing commands for mounting devices and copying files for instance) can come in real handy sometimes.
That said however, when dealing with files under command-line, say that you had to use it to backup your data because the desktop session was not working anymore, it is pretty common that one should come across files that contain spaces in their names. And unless you are aware of this simple trick, it can be quite frustrating, because for each space you have to add a backslash ( \ ) when using the command-line.
For example, let us assume that I have a file called Data backup 2.tar on my Home folder, and that I need to copy it to /media/pen location using the ‘cp’ command. If I enter the below command then I will receive an error saying that there exists no such file/directory.
sudo cp Data backup 2.tar /media/pen
Note: I am running ‘cp’ with ‘sudo’ (administrative) privileges.
In fact, if you carefully look at the output, you can see that command-line has treated the file name as three different files due to the three spaces that are present in the file name. So how can we overcome this ?
Well, you can add backslashes as mentioned before and below is the correct command using that.
sudo cp Data\ backup\ 2.tar /media/pen
But as you can see, not only it is time consuming, but since we are not that used to seeing backslashes on file names, it can be really confusing as well. So instead, you can put the file name that contains spaces, between two apostrophes. For this example, I will use the below command and the copying will be carried on without errors.
sudo cp ‘Data backup 2.tar’ /media/pen
As you see, once you add the first apostrophe, then you can type the file name freely, and when done entering the name, make sure to add another apostrophe (this is very important). That is all there is to it.
If a path contains two or more file names with spaces …
You can do the same. You can add apostrophes separately to each name, or you can put that particular path which contains names with spaces, between two apostrophes, as a whole, which is much easier. Coming back to the above example, let us say the Data backup 2.tar file is inside a folder called untitled folder that is located on my Home folder, then I will use the below command for copying it to the same destination.
sudo cp ‘untitled folder/Data backup 2.tar’ /media/pen
If both file paths (source and destination) contain such names (or even a mix — a file path that contains names with spaces and names without them), then you can add apostrophe pairs to each path, separately. Let us take the same example and say that this time we need to copy it over to /media/pen 2/new folder (as you can see this path contains two names with spaces in their names and one without).
For that I will use the below command.
sudo cp ‘ untitled folder/Data backup 2.tar ‘ ‘ /media/pen 2/new folder ‘
(Again, please remember that I have used ‘sudo’ with ‘cp’ because I am copying files to a location which requires administrative privileges. Here I have also colored the apostrophe pairs differently so it is easy to understand).
But the rule is, no matter what you do, you should treat the two paths (file source and the destination) individually. In other words, you cannot put the whole command (two file paths) inside two apostrophes, if you do so, then ‘cp’ will treat it as a the file source and will give an error saying the destination is missing.
sudo cp ‘untitled folder/Data backup 2.tar /media/pen 2/new folder’
To finish, not just with ‘cp’, you can use this trick with almost any command that requires you to deal with a file name that contain spaces under the command-line in GNU/Linux. Good luck.
Источник