Linux if device exists

One liner to check for file exists [duplicate]

Objective: Check for presence of backup .tgz file containing today’s date; output 1 for OK, 0 for no file. I’m a sucker for one liners 🙂 For example in PHP (and pretty much similar in Javascript), in various scenarios I like to do something like

Is there similar syntax in Bash? I know how to check for presence of a regular file using -f FILENAME, I only want the command to print 1 or 0 🙂

Why isn’t -f enough then? That already returns 0 or 1. Do you want the 0 or 1 to be printed or returned?

@DavDav «I’m a sucker for one liners» You do not need a line break after at the end of a PHP program.

7 Answers 7

#to check if it's a regular file [ -f "/you/file.file" ] && echo 1 || echo 0 #to check if a file exist [ -e "/you/file.file" ] && echo 1 || echo 0 

In shell this charater [ means test, -e if file exists ] end of test && if command return true execute the command after, || if command return false execute command after.
This should work in shell and bash

if [ ! -f /tmp/foo.txt ]; then echo "File not found!"; else echo "file found"; fi 

It should be noted (since the type of person asking a question like this might not think about it) that -f will only alert you of the existence of files you are allowed to see. If you don’t have sufficient permissions on a directory, you can’t see files within it, even if you specify the full path of the file.

Thanks for input — I am aware of this. In the specific case at hand, the user running the script has sufficient permissions in the particular folder.

With zsh :

Would output the number of files in the current directory whose name contains the current date in YYYY-mm-dd format and end in .tgz as a decimal number. Replace (DN) with (DN[1]) if you want only 0 or 1.

To use as the condition in an if statement, you can do:

if ()(($#)) *"$(date +%Y-%m-%d)"*.tgz(DN); then echo found else echo none found fi 

In bash

(shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; echo "$#") 

(replace «$#» with «$(($#>0))» to get 0 or 1).

if (shopt -s nullglob dotglob; set -- *"$(date +%Y-%m-%d)"*.tgz; (($#))); then echo found else echo none found fi 

With ksh93 :

(FIGNORE='@(.|..)'; set -- ~(N)*"$(date +%Y-%m-%d)"*.tgz; echo "$#") 
if (FIGNORE='@(.|..)'; set -- ~(N)*"$(date +%Y-%m-%d)"*.tgz; (("$#"))); then. 

POSIXly

ls -qA | grep -c "$(date +%Y-%m-%d).*\.tgz$" 
ls -qA | grep -q "$(date +%Y-%m-%d).*\.tgz$"; echo "$(($? == 0))" 
if ls -qA | grep -q "$(date +%Y-%m-%d).*\.tgz$"; then. 

Though the common wisdom is not to parse the output of ls , here with -q , we’re making sure there’s one file per line and the replacing of non-printable characters with ? shouldn’t affect the grep ing for our pattern so it should be relatively safe.

Читайте также:  Linux сколько строк кода

You may see differences if the file names contain sequences of bytes that don’t form valid characters. One advantage is that you’ll get an error message if the current directory is no readable.

Источник

Linux if device exists

ive written a script to backup my /home to my external drive and write a few details to a log.

but I want to have the code check if the external drive is plugged in, so something in pseudo code like..

do the rsync and write details to log file
>

how could this be done in bash?

you must need to check when the device exist something like:

-f will check if the file exist.

thanks for pointing me in right direction.
but to clarify if anyone looks at this thread for help its
if [ -d /media/disk ] as its checking for a directory not a file

I am happy you solved your problem.

Just mark this thread solved with the «Thread Tools»

Probably better to check in /etc/mtab since that file contains a list of all currently mounted devices. Something along the lines of:

if grep ‘/media/disk’ /etc/mtab > /dev/null 2>&1; then
echo «mounted»
else
echo «not mounted»
fi

Add code to the appropriate sections to do something useful other than echo messages to the screen. Good luck!

if grep ‘/media/disk’ /etc/mtab > /dev/null 2>&1; then
echo «mounted»
else
echo «not mounted»
fi

I have a flash drive that always shows as ‘E441-2317’ and it shows in the «etc/mtab» file when inserted. I want it to mount as «/flash» and so I have the code:

if grep ‘E441-2317’ /etc/mtab > /dev/null 2>&1; then
mkdir /flash
# parsing line
mount $what /flash
fi

What parsing is needed, in the «# parsing line» to get the first string set/portion of «/dev/?» for my flash drive from the grep string you are producing here, assigning it to my $what var?

Источник

Thread: bash script, if device exists

PetePete is offlineTea Glorious Tea!

bash script, if device exists

ive written a script to backup my /home to my external drive and write a few details to a log.

but I want to have the code check if the external drive is plugged in, so something in pseudo code like..

do the rsync and write details to log file
>

Читайте также:  Flash rom on linux

how could this be done in bash?

gnusci is offlineQuad Shot of Ubuntu

Re: bash script, if device exists

you must need to check when the device exist something like:

«Never make a calculation until you know the answer.» — Wheeler, Spacetime Physics, pg 60.

PetePete is offlineTea Glorious Tea!

Re: bash script, if device exists

thanks for pointing me in right direction.
but to clarify if anyone looks at this thread for help its
if [ -d /media/disk ] as its checking for a directory not a file

gnusci is offlineQuad Shot of Ubuntu

Re: bash script, if device exists

I am happy you solved your problem.

Just mark this thread solved with the «Thread Tools»

«Never make a calculation until you know the answer.» — Wheeler, Spacetime Physics, pg 60.

robert_pectol is offlineFrothy Coffee!

Re: bash script, if device exists

Probably better to check in /etc/mtab since that file contains a list of all currently mounted devices. Something along the lines of:

if grep '/media/disk' /etc/mtab > /dev/null 2>&1; then echo "mounted" else echo "not mounted" fi

Add code to the appropriate sections to do something useful other than echo messages to the screen. Good luck!

OldManRiver is offlineDipped in Ubuntu

Re: bash script, if device exists

I have a flash drive that always shows as ‘E441-2317’ and it shows in the «etc/mtab» file when inserted. I want it to mount as «/flash» and so I have the code:

if grep 'E441-2317' /etc/mtab > /dev/null 2>&1; then mkdir /flash # parsing line mount $what /flash fi

What parsing is needed, in the «# parsing line» to get the first string set/portion of «/dev/?» for my flash drive from the grep string you are producing here, assigning it to my $what var?

sisco311 is offlineDon’t PANIC!

Re: bash script, if device exists

Necromancy. I moved your post here

  • Site Areas
  • Settings
  • Private Messages
  • Subscriptions
  • Who’s Online
  • Search Forums
  • Forums Home
  • Forums
  • The Ubuntu Forum Community
    1. Ubuntu Official Flavours Support
      1. New to Ubuntu
      2. General Help
      3. Installation & Upgrades
      4. Hardware
      5. Desktop Environments
      6. Networking & Wireless
      7. Multimedia Software
    2. Ubuntu Specialised Support
      1. Ubuntu Development Version
      2. Security
      3. Virtualisation
      4. Ubuntu Servers, Cloud and Juju
        1. Server Platforms
        2. Ubuntu Cloud and Juju
      5. Gaming & Leisure
        1. Emulators
      6. Wine
      7. Development & Programming
        1. Packaging and Compiling Programs
        2. Development CD/DVD Image Testing
        3. Ubuntu Application Development
        4. Ubuntu Dev Link Forum
        5. Programming Talk
        6. Repositories & Backports
          1. Ubuntu Backports
            1. Bug Reports / Support
      8. System76 Support
      9. Apple Hardware Users
    3. Ubuntu Community Discussions
      1. Ubuntu, Linux and OS Chat
        1. Recurring Discussions
        2. Full Circle Magazine
      2. The Cafe
        1. Cafe Games
      3. Market
      4. Mobile Technology Discussions (CLOSED)
      5. Announcements & News
      6. Weekly Newsletter
      7. Membership Applications
      8. The Fridge Discussions
      9. Forum Council Agenda
      10. Forum Feedback & Help
        1. Request a LoCo forum
      11. Resolution Centre
    4. Other Discussion and Support
      1. Other OS Support and Projects
        1. Other Operating Systems
          1. Ubuntu/Debian BASED
          2. Debian
          3. MINT
          4. Arch and derivatives
          5. Fedora/RedHat and derivatives
          6. Mandriva/Mageia
          7. Slackware and derivatives
          8. openSUSE and SUSE Linux Enterprise
          9. Mac OSX
          10. PCLinuxOS
          11. Gentoo and derivatives
          12. Windows
          13. BSD
          14. Any Other OS
      2. Assistive Technology & Accessibility
      3. Art & Design
      4. Education & Science
      5. Documentation and Community Wiki Discussions
      6. Tutorials
        1. Outdated Tutorials & Tips
      7. Ubuntu Women
      8. Ubuntu LoCo Team Forums
        1. Americas LoCo Teams
          1. Argentina Team
            1. Software
            2. Hardware
            3. Comunidad
          2. Arizona Team — US
          3. Arkansas Team — US
          4. Brazil Team
          5. California Team — US
          6. Canada Team
          7. Centroamerica Team
          8. Chile Team
            1. Comunidad
            2. Hardware
            3. Software
            4. Instalaci�n y Actualizaci�n
          9. Colombia Team — Colombia
          10. Georgia Team — US
          11. Illinois Team
          12. Indiana — US
          13. Kentucky Team — US
          14. Maine Team — US
          15. Minnesota Team — US
          16. Mississippi Team — US
          17. Nebraska Team — US
          18. New Mexico Team — US
          19. New York — US
          20. North Carolina Team — US
          21. Ohio Team — US
          22. Oklahoma Team — US
          23. Oregon Team — US
          24. Pennsylvania Team — US
          25. Peru Team
          26. Texas Team — US
          27. Uruguay Team
          28. Utah Team — US
          29. Virginia Team — US
          30. West Virginia Team — US
        2. Asia and Oceania LoCo Teams
          1. Australia Team
          2. Bangladesh Team
          3. Hong Kong Team
          4. Myanmar Team
          5. Philippine Team
          6. Singapore Team
        3. Europe, Middle East, and African (EMEA) LoCo Teams
          1. Albania Team
          2. Catalan Team
          3. Portugal Team
          4. Egypt Team
          5. Georgia Team
          6. Ireland Team — Ireland
          7. Kenyan Team — Kenya
          8. Kurdish Team — Kurdistan
          9. Lebanon Team
          10. Morocco Team
          11. Saudi Arabia Team
          12. Sudan Team
          13. Tunisia Team
        4. Other Forums & Teams
        5. LoCo Archive
          1. Afghanistan Team
          2. Alabama Team — US
          3. Alaska Team — US
          4. Algerian Team
          5. Andhra Pradesh Team — India
          6. Austria Team
          7. Bangalore Team
          8. Bolivia Team
          9. Cameroon Team
          10. Colorado Team — US
          11. Connecticut Team
          12. Costa Rica Team
          13. Delhi Team
          14. Ecuador Team
          15. El Salvador Team
          16. Florida Team — US
          17. Galician LoCo Team
          18. Greek team
          19. Hawaii Team — US
          20. Honduras Team
          21. Idaho Team — US
          22. Iowa Team — US
          23. Jordan Team
          24. Kansas Team — US
          25. Libya Team
          26. Louisiana Team — US
          27. Maryland Team — US
          28. Massachusetts Team
          29. Michigan Team — US
          30. Missouri Team — US
          31. Montana Team — US
          32. Namibia Team
          33. Nevada Team — US
          34. New Hampshire Team — US
          35. New Jersey Team — US
          36. Northeastern Team — US
          37. Panama Team
          38. Paraguay Team
          39. Qatar Team
          40. Quebec Team
          41. Rhode Island Team — US
          42. Senegal Team
          43. South Carolina Team — US
          44. South Dakota Team — US
          45. Switzerland Team
          46. Tamil Team — India
          47. Tennessee Team — US
          48. Trinidad & Tobago Team
          49. Uganda Team
          50. United Kingdom Team
          51. US LoCo Teams
          52. Venezuela Team
          53. Wales Team
          54. Washington DC Team — US
          55. Washington State Team — US
          56. Wisconsin Team
          57. Yemen Team
          58. Za Team — South Africa
          59. Zimbabwe Team
Читайте также:  Настройка ip адресации linux

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Источник

Оцените статью
Adblock
detector