how to find a section of current directory path using regex in a bash function - bash

i need to just find one section of the path to be used as the parent path in a bash script function.
for example
cd = /var/www/
pwd = /var/www/alpha/something/somewhere/somewhere
pwd = /var/www/1042/someplace/somehow
now i need to find the third section of this path
eg: migration, or 1042
to be the new root so i can tell bash to look for some other child folders no matter where and how deep i'm currently at.
so that if for example im at /8 of alpha/
pwd = /var/www/alpha/1/2/3/4/5/6/7/8
/a/b/c/d/
i can issue commands to /d quickly

Quick one liner to print the directory:
pwd | cut -d / -f 4
To actually run a command from that directory, use:
(cd `pwd | cut -d / -f 1-4`; your-command-here)

Related

mv/cp commands not working as expected wih xargs in bash

Hi I have 2 parent directories with these contents, under /tmp:
Note parent directory names have ";" in it- not recommended in Unix like systems, but those directories are pushed by an external application, and that's the way we have to deal with it.
I need to move these parent directories (along with their contents) to /tmp/archive - on a RHEL 7.9 (Maipo) machine
My simple code:
ARCHIVE="/tmp/archive"
[ -d "${ARCHIVE}" ] && mkdir -p "${ARCHIVE}"
ls -lrth /tmp | awk "\$NF ~ /2021-.*/{print \$NF}" | xargs -I "{}" mv "{}" ${ARCHIVE}/
But when I run this script, mv copies one of the parent directory as it is, but for the other one, it just moves the contents of the parent directory, not the directory itself:
I tried the same script with cp -pvr command in place of mv, and its the same behavior
When I run the same script in a Ubuntu 18 system, the behavior is as expected i.e - the parent directories get moved to archive folder.
Why is there this difference in behavior between a Ubuntu and a RHEL system, for the same script
Try a simpler approach:
mkdir -p /tmp/archive
mv -v /tmp/2021-*\;*\;*/ /tmp/archive

Same script is giving result zero on cron where manually getting correct result

#!/bin/bash
cd /ad/bd/cd/dd/ed/zd
count=$(find . -type f|cut -d "/" -f3|wc -l)
echo $count >> /ad/bd/cd/abc.log
#exit
Manually it is giving correct value. i.e 230
ad/bd/script
when above cron running it is giving zero i.e 0
Try specifying absolute path of the directory in find where you want to look for files
count=$(find "/ad/bd/cd/dd/ed/zd" -type f | cut -d "/" -f3 | wc -l)
In general, cron runs your commands in your home directory. If you want to run your script from a specific directory, start that shell snippet by a command to change the directory like this:
0 10 * * * cd /some/dir && /path/to/script args
Please note that, the use of && over ; doesn’t make any difference, but if the cd command fails (for instance, because of the target directory doesn’t exist) with && your script is not executed, whereas with ; it's executed (but in an unintended directory).

Why is this bash script not changing path?

I wrote a basic script which changes the directory to a specific path and shows the list of folders, but my script shows the list of files of the current folder where my script lies instead of which I specify in script.
Here is my script:
#!/bin/bash
v1="$(ls -l | awk '/^-/{ print $NF }' | rev | cut -d "_" -f2 | rev)"
v2=/home/PS212-28695/logs/
cd $v2 && echo $v1
Does any one knows what I am doing wrong?
Your current script makes no sense really. v1 variable is NOT a command to execute as you expect, but due to $() syntax it is in fact output of ls -t at the moment of assignment and that's why you have files from current directory there as this is your working directory at that particular moment. So you should rather be doing ordinary
ls -t /home/PS212-28695/logs/
EDIT
it runs but what if i need to store the ls -t output to variable
Then this is same syntax you already had, but with proper arguments:
v1=$(ls -t /home/PS212-28695/logs/)
echo ${v1}
If for any reason you want to cd then you have to do that prior setting v1 for the same reason I explained above.

How to set a Directory as an Argument in Bash

I am having trouble finding out how to set a directory as an argument in bash.
The directory I am trying to have as an argument is /home/rrodriguez/Documents/one.
Anywhere I try to look for an answer I see examples like dir = $1 but I cant seem to find an explanation of what this means or how to set it up so that it references my specific file location. Could anyone show me how to set up my variable for my path directory?
Adding my code for a better understanding of what im trying to do:
#!bin/bash
$1 == 'home/rrodriguez/Documents/one/'
dir = $1
touch -c $dir/*
ls -la $dir
wc$dir/*
Consider:
#!bin/bash
dir=$1
touch -c "$dir"/*
ls -la "$dir"
This script takes one argument, a directory name, and touches files in that directory and then displays a directory listing. You can run it via:
bash script.sh 'home/rrodriguez/Documents/one/'
Since home/rrodriguez/Documents/one/ is the first argument to the script, it is assigned to $1 in the script.
Notes
In shell, never put spaces on either side of the = in an assignment.
I omitted the line wc$dir/* because it wasn't clear to me what the purpose of it was.
I put double-quotes around $dir to prevent the shell from, among other things, performing word-splitting. This would matter if dir contains spaces.

Get FreeSpace Available from a nonexistent Directory Path

I wanted to get the freespace available from a given path. Currently, my method is by using
df -kb $PATH | awk 'NR == 2 {print $4}'.
The problem for the above method is it won't work for a non-existing path.
So, it can be solved by mkdir -p $PATH.
But then again, it will create blank directories just to check for free spaces available (provided these path is not exist in the system earlier)
I can run rmdir $PATH after running my df, but remember? I use mkdir -p earlier and the OS may have created a few levels of directories and executing rmdir $PATH only manage to remove the last level.
So, any simple solution available just for me to check the free space from a given path(especially for nonexistent path)? or is there any utility that can output the mount point from a given directory path (so that I can just grep the mount path from df to get the available free spaces)?
If you want to know the space available for a director that you plan to create but does not exist yet, first you need to find the closest parent directory that exists:
dir_path=/some/dir
existing_path=$dir_path
while test ! -e $existing_path; do
existing_path=$(dirname $existing_path)
done
then you can df on $existing_path. All this in a single bash function:
function my_df() {
existing_path=$1
while test ! -e $existing_path; do
existing_path=$(dirname $existing_path)
done
df -k $existing_path
}
test -d $dir_path && ( df -k $dir_path | awk 'NR == 2 {print $4}' )

Resources