Script to get all file names in a directory [duplicate] - bash

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 2 years ago.
I need to get all file names for a directory
for file in "$1"/*;do
echo $file
done
gives me directory_name/file1_name, directory_name/file2_name.
However, I just want the file1_name

You can use parameter expansion to get only the filename before printing as follows:
for file in "$1"/*;do
file="${file##*/}"
echo $file
done
You can find more about parameter expansion over here : Shell Parameter Expansion (Bash Reference Manual)

Related

bash read file with a path gives error: No such file or directory [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
When to wrap quotes around a shell variable?
(5 answers)
sh read command eats backslashes in input?
(2 answers)
Closed 1 year ago.
So I am trying to read each line of a file but it gives me an error.
No such file or directory
But the file does exist
here is the code:
echo the file path is $pathToGo
while read p
do
echo $p
done < $pathToGo
output:
Now if I hard code the path it works just fine:
pathToGo="C:\Users\sorel\Bash\CW3\4\files\indexFiles\3346"
echo the file path is $pathToGo
while read p
do
echo $p
done < $pathToGo
I have also tried this code on a Linux machine, with a different path, and the same error is showing...
any help would be much appreciated.
try below : I just added -r option .
echo the file path is $pathToGo
while read -r p
do
echo $p
done < $pathToGo

bash - get directory a script is in when run from a different directory [duplicate]

This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 4 years ago.
If I'm in /home/whatever and I run, say, $ bash /home/folder/subfolder/script1.sh, how can I get /home/folder/subfolder into a variable? $0 or $(readlink -f $0) give me the whole path and filename, i.e. /home/folder/subfolder/script1.sh, which isn't what I'm after. Thanks in advance.
Use basename after getting the script name
dirname /home/folder/subfolder/script1.sh
outputs
/home/folder/subfolder
You may try this:
#!/bin/bash
p=$(dirname $0)
echo $p

In shell script, how do I use the contents of a file as a parameter [duplicate]

This question already has answers here:
Need to assign the contents of a text file to a variable in a bash script
(4 answers)
Closed 4 years ago.
Suppose in dir.txt I have the following content:
test-dir
I try to use that as a parameter as follows:
echo dir.txt | cp * $1
I want the above to be the equivalent of:
cp * test-dir
What am I doing wrong?
You are giving the string "dir.txt" to a program that does not accept any input by stdin.
You are looking for the following syntax:
cp * "$(<dir.txt)"
$() runs the command inside parenthesis and substitutes its results in its position in the command line. The < is a shorthand to read a file (a cat would also work). The quotes are to avoid problems with spaces.
You can get content of file to variable:
file1=$(cat dir.txt)
echo $file1
Results:
test-dir

Bash for-loop with input that contains embedded variable [duplicate]

This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Variables in bash seq replacement ({1..10}) [duplicate]
(7 answers)
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 4 years ago.
I am trying to write a shell script that uses wget to download files in bulk from urls that follow a certain numeric pattern.
Understandably, the url from the user input must contain the variable $i.
dl.sh http://some/url/$i/some/url 1 9
This yields repeated result from the final loop because $i will be expanded before passing down into the loop.
http://some/url/9/some/url
http://some/url/9/some/url
...
http://some/url/9/some/url
Is there a workaround to get this shell script working?
Source Code:
#!/bin/bash
# dl.sh url | index_from | index_to
for i in $(seq $2 $3)
do
echo ${1} # replace with wget for actual download.
done
Expected Result:
http://some/url/1/some/url
http://some/url/2/some/url
http://some/url/3/some/url
...
http://some/url/9/some/url

Bash Script : How to pass a wildcard before my filename in "if" exist file? [duplicate]

This question already has answers here:
Check if a file exists with a wildcard in a shell script [duplicate]
(21 answers)
Closed 6 years ago.
I wish to be able to check if file exist.
if [ -f "/var/run/screen/user/*.$InstanceName" ]; then
echo -e "screen instance exist"
fi
but the wilcard / joker don't work
How I can pass it ?
Your wildcard doesn't work because it's quoted. Unquoting it however might break the [ command as it only expects one filename argument, and if two or more files wore globbed it would break.
In bash you can use compgen that will generate a list of files matching the globbing pattern, it will also set proper exit status if no globs are found, it is a hack? I don't know, but it could look like it:
if compgen -G "/var/run/screen/user/*/$InstanceName" > /dev/null; then
printf "screen instance exist\n"
fi

Resources