Author Topic: copy files with no extension  (Read 1327 times)

0 Members and 1 Guest are viewing this topic.

Offline cabajones

  • Jr. Member
  • **
  • Posts: 1
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
copy files with no extension
« on: July 03, 2017, 01:00:29 pm »
Hi,

Can you please let me know how to copy files with no extension using shell script.I have a file of type file with no extension(.txt or .csv etc..)



Thanks
jones

Online Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 17024
  • Karma: 431
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: copy files with no extension
« Reply #1 on: July 03, 2017, 02:33:58 pm »
Maybe use
Code: [Select]
file --mime-type
to get the mime info for the file type .. so for text files it'd be:-
text/plain
and for a jpeg it'd be
image/jpeg

Then maybe something like
Code: [Select]
find /path/to/directory/to/search | while read FILE; do if [ $(file --mime-type -b "$FILE") == "text/plain" ]; then cp -v "$FILE" "/path/to/where/you/want/them/copied/$FILE"; fi; done;
and
Code: [Select]
find /path/to/directory/to/search | while read FILE; do if [ $(file --mime-type -b "$FILE") == "image/jpeg" ]; then cp -v "$FILE" "/path/to/where/you/want/them/copied/$FILE"; fi; done;
Where this is going to fall over is in your example you list txt and csv .. as far as I'm aware they are both text/plain files .. so searching for them by mime type aint gonna distinguish between them, in which case you're going to have to somehow distinguish between them by content.
WARNING: You are logged into reality as 'root'
logging in as 'insane' is the only safe option.
pcnetspec.co.uk

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal