Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move all files with fewer than 5 bytes in BASH?

Tags:

bash

mv

filesize

I have a folder containing many audio files, but some are ~4 bytes and seem to contain nothing, they are 0 seconds long and have no sound. I want to move them to a folder called "temp/".

How can I move all of the files in the folder that have fewer than 5 bytes to this folder?

like image 896
Village Avatar asked Nov 16 '13 03:11

Village


1 Answers

Find!

You can use find to do this for you:

find . -type f -maxdepth 1 -size -5c -exec mv {} temp/ \;

Explanation

-size -5c grabs all the files less than 5 bytes. The - indicates less than and the c indicates bytes.

-maxdepth 1 prevents you from trying to move the files on top of themselves when it tries to recurse into temp/ (after moving your initial files).

-exec mv {} temp/ \; simply runs mv on each file to put them in temp (the {} is substituted for the name of the file). The escaped semicolon marks the end of the mv command for exec.

There are other sizes available as well:

`b'    for  512-byte blocks (this is the default if no suffix is
       used)
`c'    for bytes
`w'    for two-byte words
`k'    for Kilobytes (units of 1024 bytes)
`M'    for Megabytes (units of 1048576 bytes)
`G'    for Gigabytes (units of 1073741824 bytes)
like image 126
Kyle Kelley Avatar answered Sep 25 '22 20:09

Kyle Kelley