Hard ==== Commands to check hard disk partitions and disk space on Linux -------------------------------------------------------------- http://www.binarytides.com/linux-command-check-disk-partitions/ df ++ Df is not a partitioning utility, but prints out details about only mounted file systems. The list generated by df even includes file systems that are not real disk partitions. Here is a simple example .. code-block:: bash $ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda6 97G 43G 49G 48% / none 4.0K 0 4.0K 0% /sys/fs/cgroup udev 3.9G 8.0K 3.9G 1% /dev tmpfs 799M 1.7M 797M 1% /run none 5.0M 0 5.0M 0% /run/lock none 3.9G 12M 3.9G 1% /run/shm none 100M 20K 100M 1% /run/user /dev/sda8 196G 154G 33G 83% /media/13f35f59-f023-4d98-b06f-9dfaebefd6c1 /dev/sda5 98G 37G 62G 38% /media/4668484A68483B47 Only the file systems that start with a /dev are actual devices or partitions. Use grep to filter out real hard disk partitions/file systems. .. code-block:: bash $ df -h | grep ^/dev /dev/sda6 97G 43G 49G 48% / /dev/sda8 196G 154G 33G 83% /media/13f35f59-f023-4d98-b06f-9dfaebefd6c1 /dev/sda5 98G 37G 62G 38% /media/4668484A68483B47 To display only real disk partitions along with partition type, use df like this .. code-block:: bash $ df -h --output=source,fstype,size,used,avail,pcent,target -x tmpfs -x devtmpfs Filesystem Type Size Used Avail Use% Mounted on /dev/sda6 ext4 97G 43G 49G 48% / /dev/sda8 ext4 196G 154G 33G 83% /media/13f35f59-f023-4d98-b06f-9dfaebefd6c1 /dev/sda5 fuseblk 98G 37G 62G 38% /media/4668484A68483B47 Note that df shows only the mounted file systems or partitions and not all. lsblk +++++ Lists out all the storage blocks, which includes disk partitions and optical drives. Details include the total size of the partition/block and the mount point if any. Does not report the used/free disk space on the partitions. .. code-block:: bash $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 465.8G 0 disk ├─sda1 8:1 0 70G 0 part ├─sda2 8:2 0 1K 0 part ├─sda5 8:5 0 97.7G 0 part /media/4668484A68483B47 ├─sda6 8:6 0 97.7G 0 part / ├─sda7 8:7 0 1.9G 0 part [SWAP] └─sda8 8:8 0 198.5G 0 part /media/13f35f59-f023-4d98-b06f-9dfaebefd6c1 sdb 8:16 1 3.8G 0 disk └─sdb1 8:17 1 3.8G 0 part sr0 11:0 1 1024M 0 rom If there is no MOUNTPOINT, then it means that the file system is not yet mounted. For cd/dvd this means that there is no disk. Lsblk is capbale of displaying more information about each device like the label and model. blkid +++++ Prints the block device (partitions and storage media) attributes like uuid and file system type. Does not report the space on the partitions. .. code-block:: bash $ sudo blkid /dev/sda1: UUID="5E38BE8B38BE6227" TYPE="ntfs" /dev/sda5: UUID="4668484A68483B47" TYPE="ntfs" /dev/sda6: UUID="6fa5a72a-ba26-4588-a103-74bb6b33a763" TYPE="ext4" /dev/sda7: UUID="94443023-34a1-4428-8f65-2fb02e571dae" TYPE="swap" /dev/sda8: UUID="13f35f59-f023-4d98-b06f-9dfaebefd6c1" TYPE="ext4" /dev/sdb1: UUID="08D1-8024" TYPE="vfat" How to check Swap space in Linux -------------------------------- This will show your allocated swap disk or disks, if any: .. code-block:: bash swapon -s Type the following command to see total and used swap size: .. code-block:: bash cat /proc/swaps This will show both your memory and your swap usage: .. code-block:: bash # Size options are: -k, -m, -g $ free -m Create iso image for swap ------------------------- https://www.digitalocean.com/community/tutorials/how-to-configure-virtual-memory-swap-file-on-a-vps .. code-block:: bash $ cd /var $ touch swap.img $ chmod 600 swap.img # to crate 512MB image file $ dd if=/dev/zero of=/var/swap.img bs=1024k count=512 $ mkswap /var/swap.img $ swapon /var/swap.img Remove swap ----------- .. code-block:: bash $ sudo swapoff /var/swap.img $ sudo rm /var/swap.img https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/3/html/System_Administration_Guide/s1-swap-removing.html Mount and UnMount usb --------------------- .. code-block:: bash $ ls -la /dev/disk/by-uuid/ $ mkdir /mnt/sdc1 $ mount /dev/sdc1 /mnt/sdc1 # to unmount $ umount /dev/sdc1 What is a symbolic link? ------------------------ A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias. Unlike a hard link,a symbolic link does not contain the data in the target file, it simply points to another entry somewhere in the file system. A hard link is where there are actually two entries in a file systems FAT table which go to the same memory location, as opposed to a symlink where one file points to another. with a hard link you can actually delete the original file, but as other links will remain, it is still available elsewhere. This difference gives symbolic links certain qualities that hard links do not have, such as the ability to link to directories, or to files on remote computers networked through NFS. Also, when you delete a target file, symbolic links to that file become unusable, whereas hard links preserve the contents of the file. To create a symbolic link in Unix, at the Unix prompt, enter: .. code-block:: bash ln -s source_file myfile Replace source_file with the name of the existing file for which you want to create the symbolic link (this file can be any existing file or directory across the file systems). Replace myfile with the name of the symbolic link. The ln command then creates the symbolic link. After you've made the symbolic link, you can perform an operation on or execute myfile, just as you could with the source_file. You can use normal file management commands (e.g., cp, rm) on the symbolic link. Note: If you delete the source file or move it to a different location, your symbolic file will not function properly. You should either delete or move it. If you try to use it for other purposes (e.g., if you try to edit or execute it), the system will send a "file nonexistent" message. What is a hard link? -------------------- A hard link is essentially a label or name assigned to a file. Conventionally, we think of a file as consisting of a set of information that has a single name. However, it is possible to create a number of different names that all refer to the same contents. Commands executed upon any of these different names will then operate upon the same file contents. To make a hard link to an existing file, enter: .. code-block:: bash ln oldfile newlink Replace oldfile with the original filename, and newlink with the additional name you'd like to use to refer to the original file. This will create a new item in your working directory, newlink, which is linked to the contents of oldfile. The new link will show up along with the rest of your filenames when you list them using the ls command. This new link is not a separate copy of the old file, but rather a different name for exactly the same file contents as the old file. Consequently, any changes you make to oldfile will be visible in newlink. You can use the standard Unix rm command to delete a link. After a link has been removed, the file contents will still exist as long as there is one name referencing the file. Thus, if you use the rm command on a filename, and a separate link exists to the same file contents, you have not really deleted the file; you can still access it through the other link. Consequently, hard links can make it difficult to keep track of files. Furthermore, hard links cannot refer to files located on different computers linked by NFS, nor can they refer to directories. For all of these reasons, you should consider using a symbolic link, also known as a soft link, instead of a hard link. Summarize disk usage of each FILE, recursively for directories -------------------------------------------------------------- -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) -s, --summarize display only a total for each argument .. code-block:: bash $ du -sh /home/or 12G /home/or $ ncdu /home/or Clean NTFS partition for windows cache files -------------------------------------------- .. code-block:: bash root@debian:/home/or# mount -a The disk contains an unclean file system (0, 0). Metadata kept in Windows cache, refused to mount. Failed to mount '/dev/sdb6': Operation not permitted The NTFS partition is in an unsafe state. Please resume and shutdown Windows fully (no hibernation or fast restarting), or mount the volume .. code-block:: bash # sudo apt-get install ntfsprogs # sudo ntfsfix /dev/sda3 ntfsfix is a utility that fixes some common NTFS problems. ntfsfix is NOT a Linux version of chkdsk. It only repairs some fundamental NTFS inconsistencies, resets the NTFS journal file and schedules an NTFS consistency check for the first boot into Windows. You may run ntfsfix on an NTFS volume if you think it was damaged by Windows or some other way and it cannot be mounted. * http://askubuntu.com/questions/313872/ubuntu-13-04-is-unable-to-mount-a-disk-drive-from-ex-windows-system * http://askubuntu.com/a/71205/237607 Make sub directory ------------------ .. code-block:: bash $ mkdir -p /new/sub/folder How to Sort Folders by Size With One Command Line in Linux ---------------------------------------------------------- .. code-block:: bash $ du --max-depth=1 /home/ | sort -n -r $ du -H --max-depth=1 /home/user $ du -h --max-depth=1 | sort -hr http://www.ducea.com/2006/05/14/tip-how-to-sort-folders-by-size-with-one-command-line-in-linux/ http://unix.stackexchange.com/questions/185764/how-do-i-get-the-size-of-a-directory-on-the-command-line How to Free Up a Lot of Disk Space by Deleting Cached Package Files ------------------------------------------------------------------- .. code-block:: bash $ sudo du -h /var/cache/apt/archives $ sudo apt-get clean Disable Automatic Package Caching If you’d rather not have to go in and clean out the cache folders all the time, you can tell Ubuntu to stop keeping them around with a simple configuration change. Head into System –> Administration –> Synaptic Package Manager. Then choose Settings –> Preferences Switch over to the Files tab, where you can change the option to “Delete downloaded packages after installation”, which will prevent the caching entirely. Mount unknown filesystem exfat ------------------------------ .. code-block:: bash $ sudo apt-get install exfat-fuse exfat-utils Determine the total size of a directory --------------------------------------- .. code-block:: bash du -hsc * | sort -hr .. code-block:: bash sudo apt-get install ncdu ncdu /