How to move files if directory is not empty? - bash

I am trying to move files from a directory if it is not empty. This script is added in cronjob but it is always executing regardless of files are present or not? What is wrong in this thing?
#!/bin/bash
logFolder="/dstDir/`date '+%Y-%m-%d/%H-%M'`"
tempLogFolder="sourceDir"
if [ -z "$(ls -A $tempLogFolder | grep *.log)" ]; then
mkdir -p $logFolder
mv $tempLogFolder/*.log $logFolder/
fi

Don't parse the output of ls. You don't need any external commands to count files in a directory. The shell can do it itself.
Try creating a helper function that checks how many arguments are passed to it. Then have the shell expand the glob "$tempLogFolder"/*.log. No need for ls or grep. The only trick is enabling the nullglob option so if no files exist the glob expands to nothing.
files_exist() { (($# > 0)); }
shopt -s nullglob
if files_exist "$tempLogFolder"/*.log; then
mkdir -p "$logFolder"
mv "$tempLogFolder"/*.log "$logFolder"/
fi

grep accepts regex, not glob. If you want to use glob like *.log you put it in your ls something like ls .... *.log (using ls output is not 100% safe).
If you want to use grep, please use regex, I guess what you meant is \.log$.
If you want to check if grep has found matches, you should check the return code ($?) of grep, instead of using -z test.
If you put it in crontab, the script will always be executed.

I think you want,
if [ -n "$(ls -A $tempLogFolder | grep *.log)" ]; then
...
because you are only interested in whether the string returned has a non-zero length.

Related

How to check if a file exists or not and create/delete if does/does not exist in shell

In shell, I want to check if a file exists or not then create if it doesn't exist or delete if it exists. For this I need a one liner and am trying to do something like:
ls | awk '\filename\' <if exist delete else create>
I need the ls as my problem has some command that outputs a list of strings that need to be pipelined to awk then possibly touch/mkdir.
#!/bin/bash
if [ -z "$1" ] || [ ! -f "$1" ] # $1 is input filename and -f check if $1 is a regular file
then
rm "$1" #delete the file
else
touch "$1" #create the file
fi
save the file as filecreator.sh
change the permission to allow execution with sudo chmod a+rx
while running the script use ./filecreator.sh yourfile.extension
You can see the file in your directory.
Using oc projects and oc new-project instad of ls and touch as indicated in a comment.
oc projects |
while read -r proj; do
if [ -d "$proj" ]; then
rm -rf "$proj"
else
oc new-project "$proj"
fi
done
I don't think there is a useful way to write this as a one-liner. If you like, you can replace the newlines with semicolons, except after then and else.
You really should put your actual requirements in the question itself. ls is a superbly useless example because it cannot list a file which doesn't already exist, and you should not use ls in scripts at all.
rm yourfile 2>/dev/null || touch yourfile
If the file existed before, rm will succeed and erase the file, and the touch won't be executed. You end up with no file afterwards.
If the file did not exist before, rm will fail (but the error message is not visible, since it is directed to the bitbucket), and due to the non-zero exit code of rm, the touch will be executed. You end up with an empty file afterwards.
Caveat: If the file exists, but you don't have permissions to remove it, you won't notice this error, due to the redirection of stderr. Hence, for debugging and later diagnosis, it might be better to redirect stderr to some file instead.

Associative array, file names refering to the path, for dmenu

And I started playing with dmenu and it seems such an automation for almost every thing. Unfortunately I'm not familiar with bash and it should be on my list.
I have a folder for my markdowns with subfolders containing my files. I'm trying to have a script to show them in dmenu while using an alias.
If the path to a file is
/home/user/docs/markdown/practice01/rmd/network.rmd
I would like to have
network
as an option in my dmenu. So when I choose
network -----> /home/user/docs/markdown/practice01/rmd/network.rmd
Here is my broken script. There are a few things I'm missing.
This way I get full path on my dmenu which i don't need. I tried to read about associative arrays but I can't figure it out in bash.
This script works but in case I decide to ESC and exit, still it opens up an empty vim in my directory. Hence, I should know if statements huh!
#!/bin/bash
DMenu=("dmenu -l 10 -i -nb "#eaeaea" -sb "#E53935" -nf "#474747"")
cd ~/docs/markdown/
target=$(find -type f -name '*.rmd' | $DMenu)
st vim "$target"
I made a little example. But the problem is that it is a manual work to add each file, which definitely we don't wanna do right!
#!/bin/bash
declare -A dotfiles
dotfiles[i3]="/home/user/dotfiles/i3/.config/i3/config"
dotfiles[vimrc]="/home/user/dotfiles/vim/.vimrc"
list=("i3\nvimrc")
target=$(echo -e $list | dmenu -i -nb "#eaeaea" -sb "#E53935" -nf "#474747")
st vim "${dotfiles["$target"]}"
Thank you
Associative arrays can be weird... but returning output to a variable makes it easier to manipulate as any other string in bash, as shown in the example below:
prefix="$HOME/git/notes"
suffix=".md"
shopt -s nullglob globstar
item=( "$prefix"/**/*${suffix}) # Search *.md in all dirs/subdirs
item=( "${item[#]#"$prefix"/}" )
item=( "${item[#]%${suffix}}" ) # Removes '.md' string from item name
result=$(printf '%s\n' "${item[#]}" | dmenu)
[[ -n $result ]] || exit # exit if nothing is found
gedit "${prefix}/${result}.md" # Open file by adding again '.md'
When the percent sign (%) is used in the pattern ${variable%substring}, it will return content of the variable with the shortest occurrence of substring deleted from the back of the variable.
Listed below for reference are 2 examples I wrote, one in Bash and the other in Python, for managing pass and markdown notes with dmenu:
dmenu-pass.sh
dmenu-launch.py
Also, listed below are a couple nice articles that might help you out:
The weird, wondrous world of Bash arrays
Advanced Bash-Scripting Guide: Manipulating Strings
Instead of putting some code in an array, use a function!
my_dmenu() {
dmenu -l 10 -i -nb "#eaeaea" -sb "#e53935" -nf "#474747"
}
If your markdown files are all in the same folder (and not in subfolders), you certainly don't need find: use a glob instead! and if your files are in subfolders, use a glob instead (with the globstar shell option).
All in all:
#!/bin/bash
my_dmenu() {
dmenu -l 10 -i -nb "#eaeaea" -sb "#e53935" -nf "#474747"
}
base_dir=~/docs/markdown
# Also, check the return code of cd!
cd "$base_dir" || { echo >&2 "Can't cd to $base_dir. Exiting"; exit 1; }
# Using a glob: use the shell option nullglob
shopt -s nullglob
files=( *.rmd )
# Check that there are some files found:
if (( ${#files[#]} == 0 )); then
echo "No files found. Exiting."
exit 1
fi
# Now we're ready to send the files to dmenu:
chosen_file=$(printf '%s\n' "${files[#]}" | my_dmenu)
# If dmenu returns nothing: don't launch vim!
if [[ ! $chosen_file ]]; then
echo "No files selected. Exiting."
exit 1
fi
# Now you can launch vim!
st vim "$chosen_file"
If you also want to find the *.rmd files in subfolders: use instead:
shopt -s nullglob globstar
files=( **/*.rmd )
Edit to address the requirement in your comment (and the edit of your question):
If you want to strip the .rmd suffix to show in dmenu, use:
chosen_file=$(printf '%s\n' "${files[#]%.rmd}" | my_dmenu)
# ...
st vim "$chosen_file.rmd"
The expansion ${files[#]%.rmd} will strip the suffix .rmd from each field of the array files. Don't forget to add this suffix back when you edit the file (as shown in the last line).
dmenuoptions="-l 10 -i -nb '#eaeaea' -sb '#E53935' -nf '#474747'"
st -e vim $(find ~/docs/markdown -type f -name '*.rmd' | dmenu $dmenuoptions)

Bash Script to run against all .log files

I currently have a bash script called (log2csv). While in the current directory I can type in terminal:
log2csv *.log
This will run the script on every .log file in the current directory.
Alternatively I can run it against a single .log file with
log2csv test1.log
Instead of typing log2csv *.log, can I have the *.log included in the script? So I can just type log2csv in the directory and it runs. I know I can alias that, but I rather have the script do it.
Here is the bash script I am running:
#!/bin/bash
for path
do
base=$(basename "$path")
noext="${base/.log}"
[ -e "${noext}.csv" ] && continue
/Users/joshuacarter/bin/read_scalepack.pl "$path" > "${noext}.csv"
done
Change:
for path
to:
for path in *.log
or, perhaps better:
names=( "$#" )
if [ "${#names}" = 0 ]
then names=( *.log )
fi
for path in "${names[#]}"
and you can consider whether to set options such as shopt -s nullglob as well. This uses shell arrays to handle names with blanks etc in them. It uses command line arguments if any are given, and the list of files from *.log being expanded if there are no command line arguments.

How-To get root directory of given path in bash?

My script:
#!/usr/bin/env bash
PATH=/home/user/example/foo/bar
mkdir -p /tmp/backup$PATH
And now I want to get first folder of "$PATH": /home/
cd /tmp/backup
rm -rf ./home/
cd - > /dev/null
How can I always detect the first folder like the example above? "dirname $PATH" just returns "/home/user/example/foo/".
Thanks in advance! :)
I've found a solution:
#/usr/bin/env bash
DIRECTORY="/home/user/example/foo/bar"
BASE_DIRECTORY=$(echo "$DIRECTORY" | cut -d "/" -f2)
echo "#$BASE_DIRECTORY#";
This returns always the first directory. In this example it would return following:
#home#
Thanks to #condorwasabi for his idea with awk! :)
You can try this awk command:
basedirectory=$(echo "$PATH" | awk -F "/" '{print $2}')
At this point basedirectory will be the string home
Then you write:
rm -rf ./"$basedirectory"/
If PATH always has an absolute form you can do tricks like
ROOT=${PATH#/} ROOT=/${ROOT%%/*}
Or
IFS=/ read -ra T <<< "$PATH"
ROOT=/${T[1]}
However I should also add to that that it's better to use other variables and not to use PATH as it would alter your search directories for binary files, unless you really intend to.
Also you can opt to convert your path to absolute form through readlink -f or readlink -m:
ABS=$(readlink -m "$PATH")
You can also refer to my function getabspath.
To get the first directory component of VAR:
echo ${VAR%${VAR#/*/}}
So, if VAR="/path/to/foo", this returns /path/.
Explanation:
${VAR#X} strips off the prefix X and returns the remainder. So if VAR=/path/to/foo, then /*/ matches the prefix /path/ and the expression returns the suffix to/foo.
${VAR%X} strips off the suffix X. By inserting the output of ${VAR#X}, it strips off the suffix and returns the prefix.
If you can guarantee that your paths are well formed this is a convenient method. It won't work well for some paths, such as //path/to/foo or path/to/foo, but you can handle such cases by breaking down the strings further.
To get the first firectory:
path=/home/user/example/foo/bar
mkdir -p "/tmp/backup$path"
cd /tmp/backup
arr=( */ )
echo "${arr[0]}"
PS: Never use PATH variable in your script as it will overrider default PATH and you script won't be able to execute many system utilities
EDIT: Probably this should work for you:
IFS=/ && set -- $path; echo "$2"
home
Pure bash:
DIR="/home/user/example/foo/bar"
[[ "$DIR" =~ ^[/][^/]+ ]] && printf "$BASH_REMATCH"
Easy to tweak the regex.
You can use dirname...
#/usr/bin/env bash
DIRECTORY="/home/user/example/foo/bar"
BASE_DIRECTORY=$(dirname "${DIRECTORY}")
echo "#$BASE_DIRECTORY#";
Outputs the following...
/home/user/example/foo

bash - recursive script can't see files in sub directory

I got a recursive script which iterates a list of names, some of which are files and some are directories.
If it's a (non-empty) directory, I should call the script again with all of the files in the directory and check if they are legal.
The part of the code making the recursive call:
if [[ -d $var ]] ; then
if [ "$(ls -A $var)" ]; then
./validate `ls $var`
fi
fi
The part of code checking if the files are legal:
if [[ -f $var ]]; then
some code
fi
But, after making the recursive calls, I can no longer check any of the files inside that directory, because they are not in the same directory as the main script, the -f $var if cannot see them.
Any suggestion how can I still see them and use them?
Why not use find? Simple and easy solution to the problem.
Always quote variables, you never known when you will find a file or directory name with spaces
shopt -s nullglob
if [[ -d "$path" ]] ; then
contents=( "$path"/* )
if (( ${#contents[#]} > 0 )); then
"$0" "${contents[#]}"
fi
fi
you're re-inventing find
of course, var is a lousy variable name
if you're recursively calling the script, you don't need to hard-code the script name.
you should consider putting the logic into a function in the script, and the function can recursively call itself, instead of having to spawn an new process to invoke the shell script each time. If you do this, use $FUNCNAME instead of "$0"
A few people have mentioned how find might solve this problem, I just wanted to show how that might be done:
find /yourdirectory -type f -exec ./validate {} +;
This will find all regular files in yourdirectory and recursively in all its sub-directories, and return their paths as arguments to ./validate. The {} is expanded to the paths of the files that find locates within yourdirectory. The + at the end means that each call to validate will be on a large number of files, instead of calling it individually on each file (wherein the + is replaced with a \), this provides a huge speedup sometimes.
One option is to change directory (carefully) into the sub-directory:
if [[ -d "$var" ]] ; then
if [ "$(ls -A $var)" ]; then
(cd "$var"; exec ./validate $(ls))
fi
fi
The outer parentheses start a new shell so the cd command does not affect the main shell. The exec replaces the original shell with (a new copy of) the validate script. Using $(...) instead of back-ticks is sensible. In general, it is sensible to enclose variable names in double quotes when they refer to file names that might contain spaces (but see below). The $(ls) will list the files in the directory.
Heaven help you with the ls commands if any file names or directory names contain spaces; you should probably be using * glob expansion instead. Note that a directory containing a single file with a name such as -n would trigger a syntax error in your script.
Corrigendum
As Jens noted in a comment, the location of the shell script (validate) has to be adjusted as you descend the directory hierarchy. The simplest mechanism is to have the script on your PATH, so you can write exec validate or even exec $0 instead of exec ./validate. Failing that, you need to adjust the value of $0 — assuming your shell leaves $0 as a relative path and doesn't mess around with converting it to an absolute path. So, a revised version of the code fragment might be:
# For validate on PATH or absolute name in $0
if [[ -d "$var" ]] ; then
if [ "$(ls -A $var)" ]; then
(cd "$var"; exec $0 $(ls))
fi
fi
or:
# For validate not on PATH and relative name in $0
if [[ -d "$var" ]] ; then
if [ "$(ls -A $var)" ]; then
(cd "$var"; exec ../$0 $(ls))
fi
fi

Resources