LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 08-24-2016, 06:54 PM   #1
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Rep: Reputation: 169Reputation: 169
Delete file based on file size


I am interested in a script that would delete a file based on it's file size.

Ex.

Delete any .sh files that are less than 58 bytes.
 
Old 08-24-2016, 07:07 PM   #2
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
This come close, it find files less than 57 bytes.

I need it to find files that are exactly 57 bytes.

Quote:
sudo find . -name "*.sh" -size -57w
 
Old 08-24-2016, 07:27 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by Fixit7 View Post
This come close, it find files less than 57 bytes.

I need it to find files that are exactly 57 bytes.
Code:
find . -name "*.sh" -size -58w -size +56w
Although I think +56c, -58c would be for bytes...? (I show w figuring that you know you want 2-byte words)

See michalek below... I didn't realize that, but is in line with other find options.

Last edited by astrogeek; 08-24-2016 at 07:33 PM.
 
Old 08-24-2016, 07:30 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,750

Rep: Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929Reputation: 5929
-size +xx will find files larger
-size xx will find exact file size
-size -xx will find files smaller

c is for bytes and w for two byte words.

Last edited by michaelk; 08-24-2016 at 07:32 PM.
 
1 members found this post helpful.
Old 08-24-2016, 08:27 PM   #5
Fixit7
Senior Member
 
Registered: Mar 2014
Location: El Lago, Texas
Distribution: Ubuntu_Mate 16.04
Posts: 1,374

Original Poster
Rep: Reputation: 169Reputation: 169
Thanks.

This is my finished script.

Quote:
#!/bin/bash
# Ubuntu_Mate
#
# Create script template that includes commented date and time
#
# Saves a lot of typing :-)
# Delete scripts of exactly 57 bytes
# Those files are scripts that contain no bash commands
find . -name "*.sh" -size 57c -delete
# Create a script template that includes a commented date and time
echo '#!/bin/bash' > new.sh
echo "# Ubuntu_Mate" >> new.sh
echo '#' $(date) >> new.sh
# Open geany editor and create new script
geany new.sh
# If no file changes, delete new.sh
find . -name "*.sh" -size 57c -delete
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Delete multiple lines in a file based on the input text m.praki Linux - Newbie 1 07-19-2012 06:49 AM
Delete files based on file name s_linux SUSE / openSUSE 1 06-14-2010 01:47 PM
delete a file based on content and date ElectroLinux Linux - Newbie 7 08-19-2009 07:01 AM
Script to delete files with 0k file size in a directory justgiver Linux - Newbie 4 01-28-2008 04:56 AM
search for files based on file size fatrandy13 Linux - General 1 12-05-2004 10:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 11:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration