- One liner to check for file exists [duplicate]
- 7 Answers 7
- With zsh :
- In bash
- With ksh93 :
- POSIXly
- Linux if device exists
- Thread: bash script, if device exists
- bash script, if device exists
- Re: bash script, if device exists
- Re: bash script, if device exists
- Re: bash script, if device exists
- Re: bash script, if device exists
- Re: bash script, if device exists
- Re: bash script, if device exists
- Bookmarks
- Posting Permissions
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.?php>
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.
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
Tea 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
>
how could this be done in bash?
Quad 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.
Tea 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
Quad 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.
Frothy 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!
Dipped 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?
Don’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
- Ubuntu Official Flavours Support
- New to Ubuntu
- General Help
- Installation & Upgrades
- Hardware
- Desktop Environments
- Networking & Wireless
- Multimedia Software
- Ubuntu Specialised Support
- Ubuntu Development Version
- Security
- Virtualisation
- Ubuntu Servers, Cloud and Juju
- Server Platforms
- Ubuntu Cloud and Juju
- Gaming & Leisure
- Emulators
- Wine
- Development & Programming
- Packaging and Compiling Programs
- Development CD/DVD Image Testing
- Ubuntu Application Development
- Ubuntu Dev Link Forum
- Programming Talk
- Repositories & Backports
- Ubuntu Backports
- Bug Reports / Support
- Ubuntu Backports
- System76 Support
- Apple Hardware Users
- Ubuntu Community Discussions
- Ubuntu, Linux and OS Chat
- Recurring Discussions
- Full Circle Magazine
- The Cafe
- Cafe Games
- Market
- Mobile Technology Discussions (CLOSED)
- Announcements & News
- Weekly Newsletter
- Membership Applications
- The Fridge Discussions
- Forum Council Agenda
- Forum Feedback & Help
- Request a LoCo forum
- Resolution Centre
- Ubuntu, Linux and OS Chat
- Other Discussion and Support
- Other OS Support and Projects
- Other Operating Systems
- Ubuntu/Debian BASED
- Debian
- MINT
- Arch and derivatives
- Fedora/RedHat and derivatives
- Mandriva/Mageia
- Slackware and derivatives
- openSUSE and SUSE Linux Enterprise
- Mac OSX
- PCLinuxOS
- Gentoo and derivatives
- Windows
- BSD
- Any Other OS
- Other Operating Systems
- Assistive Technology & Accessibility
- Art & Design
- Education & Science
- Documentation and Community Wiki Discussions
- Tutorials
- Outdated Tutorials & Tips
- Ubuntu Women
- Ubuntu LoCo Team Forums
- Americas LoCo Teams
- Argentina Team
- Software
- Hardware
- Comunidad
- Arizona Team — US
- Arkansas Team — US
- Brazil Team
- California Team — US
- Canada Team
- Centroamerica Team
- Chile Team
- Comunidad
- Hardware
- Software
- Instalaci�n y Actualizaci�n
- Colombia Team — Colombia
- Georgia Team — US
- Illinois Team
- Indiana — US
- Kentucky Team — US
- Maine Team — US
- Minnesota Team — US
- Mississippi Team — US
- Nebraska Team — US
- New Mexico Team — US
- New York — US
- North Carolina Team — US
- Ohio Team — US
- Oklahoma Team — US
- Oregon Team — US
- Pennsylvania Team — US
- Peru Team
- Texas Team — US
- Uruguay Team
- Utah Team — US
- Virginia Team — US
- West Virginia Team — US
- Argentina Team
- Asia and Oceania LoCo Teams
- Australia Team
- Bangladesh Team
- Hong Kong Team
- Myanmar Team
- Philippine Team
- Singapore Team
- Europe, Middle East, and African (EMEA) LoCo Teams
- Albania Team
- Catalan Team
- Portugal Team
- Egypt Team
- Georgia Team
- Ireland Team — Ireland
- Kenyan Team — Kenya
- Kurdish Team — Kurdistan
- Lebanon Team
- Morocco Team
- Saudi Arabia Team
- Sudan Team
- Tunisia Team
- Other Forums & Teams
- LoCo Archive
- Afghanistan Team
- Alabama Team — US
- Alaska Team — US
- Algerian Team
- Andhra Pradesh Team — India
- Austria Team
- Bangalore Team
- Bolivia Team
- Cameroon Team
- Colorado Team — US
- Connecticut Team
- Costa Rica Team
- Delhi Team
- Ecuador Team
- El Salvador Team
- Florida Team — US
- Galician LoCo Team
- Greek team
- Hawaii Team — US
- Honduras Team
- Idaho Team — US
- Iowa Team — US
- Jordan Team
- Kansas Team — US
- Libya Team
- Louisiana Team — US
- Maryland Team — US
- Massachusetts Team
- Michigan Team — US
- Missouri Team — US
- Montana Team — US
- Namibia Team
- Nevada Team — US
- New Hampshire Team — US
- New Jersey Team — US
- Northeastern Team — US
- Panama Team
- Paraguay Team
- Qatar Team
- Quebec Team
- Rhode Island Team — US
- Senegal Team
- South Carolina Team — US
- South Dakota Team — US
- Switzerland Team
- Tamil Team — India
- Tennessee Team — US
- Trinidad & Tobago Team
- Uganda Team
- United Kingdom Team
- US LoCo Teams
- Venezuela Team
- Wales Team
- Washington DC Team — US
- Washington State Team — US
- Wisconsin Team
- Yemen Team
- Za Team — South Africa
- Zimbabwe Team
- Americas LoCo Teams
- Other OS Support and Projects
- Ubuntu Official Flavours Support
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