Copying Files with Wildcard (*) to a Folder in a Bash Script - Why Isn't It Working

Copying files with wildcard (*) to a folder in a bash script - why isn't it working?

You got that exactly backwards -- everything except the * character should be double-quoted:

#!/bin/sh
dir_name=files

root=..
food_dir=food
fruits_dir=fruits

rm -rf "$dir_name"
mkdir "$dir_name"
chmod 755 "$dir_name"

cp "$root/$food_dir/"* "$dir_name/"

Also, as a matter of best-practice / convention, non-environment variable names should be lower case to avoid name conflicts with environment variables and builtins.

Copying Files with Wildcards in the Path

No - you cannot do that with COPY or XCOPY. There is a fundamental flaw with what you are asking.

What if you also have a directory named "boston\marathon"? Which directory do you want to copy from?

You might answer - "I want both". But now you have a nasty potential problem. What if both source directories contain a file named "test.txt". You can only have one file named "test.txt" in your destination. Which one do you keep? There is no correct answer, and that is a good reason why the command is designed to disallow what you are attempting to do.

Addendum

If you are willing to assume that only one folder matches the pattern, then you already know all the steps to accomplish your goal with the following.

@echo off
cd bo*\ma*
xcopy * c:\somedirectory

If you want to return to where you started, then use PUSHD/POPD instead of CD

@echo off
pushd bo*\ma*
xcopy * c:\somedirectory
popd

BUT, you should be very careful. Boston\marathon could exist, and neither PUSHD nor CD will give any indication that there is a potential problem.

Using wildcard character in bash scripting

i dont know about sqlldr, but i think you can try:

#!/bin/bash
FILENAME = '/u02/logs/$(date -d '2 days ago' +%Y-%m-%d)*.csv'

# LOAD CSV FILE USING SQL*LOADER
for fname in $(ls $FILENAME); do
sqlldr username/password@localhost control=control.ctl data=$fname
done

hope it helps

Bash wildcard working in command line but not script

The best answer to my question was the edit I made to my question. I'll say it again.

I wanted to use an ls wildcard in a bash script and wrote this file:

#! /bin/bash

root="[root]"
ls "$root/*"

But, writing my ls this way looks for files or directories with the name *. In order to make use of the wildcard I need to leave * outside of the quotes. See the example below:

#! /bin/bash

root="[root]"
ls "$root/"*

note:

I'm using the square brackets [] in my question and answer contextually. The name of my directory isn't the literal string [root], it's something else, but the square brackets let the reader understand what's being said contextually.

But maybe a fake root path or name is better in the future.

How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

In Bash you can do it by enabling the extglob option, like this (replace ls with cp and add the target directory, of course)

~/foobar> shopt extglob
extglob off
~/foobar> ls
abar afoo bbar bfoo
~/foobar> ls !(b*)
-bash: !: event not found
~/foobar> shopt -s extglob # Enables extglob
~/foobar> ls !(b*)
abar afoo
~/foobar> ls !(a*)
bbar bfoo
~/foobar> ls !(*foo)
abar bbar

You can later disable extglob with

shopt -u extglob


Related Topics



Leave a reply



Submit