Linux / UNIX Tech Support Forum
This is a discussion on Test Linux file exists within the Getting started tutorials forums, part of the Linux Getting Started category; You can easily test if a file exists under Linux operating system using test command. Display a message if file ...
|
|||||||
| Getting started tutorials So much to read, so little time! If that is your problem, we have solution. Read our FAQ and tutorials to help you cut through the clutter of information overload. Only members of "contributors" group can post new tutorials. Other members can just reply to thread. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
You can easily test if a file exists under Linux operating system using test command.
Display a message if file /etc/passwd exists: Code:
test -f /etc/passwd && echo 'File exists' Code:
test -f /etc/passwd2 && echo 'File exists' || echo 'File not found' Code:
[ -f /path/file.txt ] && echo 'File exists' || echo 'File not found' Code:
if [ -f /etc/passwd ]
then
echo 'Adding user to database...'
else
echo 'File not found!'
fi
* Find out if file exists with conditional expressions * man test
__________________
Raj Linux rulz. I have never turned back in my life ; I shall not do so today.. haha |
| Sponsored Links | ||
|
|
|
|||
|
Urgent!
I am having an issue with this. We are switching from UNIX to Linux, so I am going through programs making sure they work on Linux. I am having the following problem: I run a .prog file that, in turn, runs a PL/SQL program that creates one or more output files that all begin with the same 7 characters, and have similar to the following format: 411n9n_xxx_xxx_timestamp Once back in the .prog file, my code is as follows: (I have already changed single brackets to double bracket because of syntax error) Code:
if [[ -f ${EXPORTDIR}/411n9n* ]]
then
xxx
fi
Thank you! Last edited by thePITman; 18-11-2008 at 09:45 PM. |
|
|||
|
Arrgggh!!! It works now!
The whole problem was the double brackets. However, when it was just single brackets when I first started testing, it gave me an error about "too many" or "not enough arguments" but the error went away when I made it double brackets instead of just one. So the whole time there was nothing wrong with my original code! What a waste of time. Haha! |
|
|||
|
I have a similar issue - We have a Windows based database that is backed up to a linux server (Debian). The file names are similar but with a date suffix. What I need to do is confirm that a particular backup file - or set of files - is present (meaning backup completed) and then process. Script follows:
#!/bin/bash FILE="/media/Backup/SYS_0*" if [ -f $FILE ]; then rm -f /media/Backup/SYS_3* rename -f 's/SYS_2/SYS_3/' /media/Backup/SYS* rename -f 's/SYS_1/SYS_2/' /media/Backup/SYS* rename -f 's/SYS_0/SYS_1/' /media/Backup/SYS* fi The problem is that multiple SYS_0... files can exist. The script works fine if only one exists. What am I doing wrong? I have tried thePITman suggestion above but it does not work for me - if [[ -f $FILE ]]... Thanks for any help. |
![]() |
| Tags |
| files , shell scripting , test |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linux convert doc file to pdf file | suneel243 | CentOS / RHEL / Fedora | 0 | 16-10-2008 10:27 AM |
| Find Unix Linux File / Directory by date And Then Copy / Move File | asim.mcp | CentOS / RHEL / Fedora | 1 | 10-08-2008 03:30 AM |
| Check file exists on Remote Server | sanjus | Shell scripting | 3 | 12-06-2008 06:05 AM |
| script on how to check if the file exists | warren | Shell scripting | 4 | 14-04-2006 05:51 PM |
| freebsd ifconfig alias up ioctl siocaifaddr file exists erro | ZigZag | All about FreeBSD/OpenBSD/NetBSD | 1 | 23-12-2005 04:49 PM |