Author Topic: Using the "du" command with a file-list [SOLVED]  (Read 10025 times)

0 Members and 1 Guest are viewing this topic.

Offline Keith

  • Hero Member
  • *****
  • Posts: 937
  • Karma: 11
  • Gender: Male
  • Linux Novice
    • View Profile
    • Awards
Using the "du" command with a file-list [SOLVED]
« on: March 27, 2019, 09:11:31 pm »
As part of a backup script I want to find the total GB of a list of files to be backed up to see if the supplied USB stick is big enough.
The ideal command is du, for example:
keith@dt05:~$ du -sch Documents Desktop Pictures
2.4G   Documents
73M   Desktop
1.9G   Pictures
4.4G   total
keith@dt05:~$

Better still, du has an option to read the file list from a file:
--files0-from=<file-list-path>
but the listed file/directory names must be NUL-terminated, and this is where I have become stuck.

A web search indicates "\0" as a null character but adding printf "\0" after a name when creating the file-list does  not work, and I've tried many other variations offered on the web. 
I would be grateful if anyone can suggest a way of listing files/directories in a file so that du will read it.
« Last Edit: March 27, 2019, 10:02:23 pm by Keith »

Offline Keith

  • Hero Member
  • *****
  • Posts: 937
  • Karma: 11
  • Gender: Male
  • Linux Novice
    • View Profile
    • Awards
Re: Using the "du" command with a file-list
« Reply #1 on: March 27, 2019, 10:02:04 pm »
Solved!

Example:
keith@dt05:~$ more .bin/backup_file_list
Desktop
Documents
Dropbox
Music
Pictures
Programming
Radio
.fonts
.bin
keith@dt05:~$ readarray -t list < .bin/backup_file_list
keith@dt05:~$ du -sc ${list[\@]}   (NB ignore the "\" used to display the "at" symbol)
74596   Desktop
2463444   Documents
142148   Dropbox
6730260   Music
1981068   Pictures
1484   Programming
36   Radio
224   .fonts
708   .bin
11393968   total
keith@dt05:~$

Offline Keith

  • Hero Member
  • *****
  • Posts: 937
  • Karma: 11
  • Gender: Male
  • Linux Novice
    • View Profile
    • Awards
Re: Using the "du" command with a file-list [SOLVED]
« Reply #2 on: March 27, 2019, 10:29:34 pm »
Easier still:

keith@dt05:~$ echo $(du -c $(cat .bin/backup_file_list) | tail -1 | cut -f 1)
11393972
keith@dt05:~$

This hasn't solved the more elegant method of using the du command option --files0-from=<file-list-path> but it does the job. 
« Last Edit: March 28, 2019, 04:28:54 pm by Keith »

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal