Linux mkdir, find and mv - linux

So guys, I have to write this script in Linux. I have to create a directory and see if it already exists or not and then I have to find all the file which end in ".c" and move them to the directory I created.
This is what I have so far:
#!/bin/bash
while
echo "name of the directory you want to create "
read -p "$name"; do
if [ ! -d "$name" ]; then
{
echo "Directory doesn't exist. Create: "
read -p "$name"
mkdir -p Scripts/"$name"
}
else
echo "Directory exists"
fi
find ./ -name '*.c' | xargs mv -t "$name"
done
When I try to execute it, it doesn't work. It doesn't create a new directory and also it says:
mv: failed to access '': No such file or directory.
Can you please help me find a solution to this?

I'm not exactly sure what you're trying to achieve. But there are a few things in your script that don't make sense.
What do you need a while loop for? Once you've made the folder and moved all scripts, there's no sense in running it again.
Why do you read the name of the directory twice? You can just read it once, store it in $name and use it until the end of the script.
You don't need find *.c will select all files ending with .c in the current directory.
With all that said, here's the script that does what you requested if I understood correctly.
#! /bin/bash
echo -n "Enter the name of the directory you want to create: "
read name
if [ ! -d Scripts/"$name" ]; then
echo "Directory doesn't exist. Creating it."
mkdir -p Scripts/"$name"
else
echo "Directory exists"
fi
echo "Moving files"
mv *.c Scripts/"$name"

Related

Displaying file content with bash scripting

I am trying to write a bash script (display) that will allow me to access a directory, list the files, and then display the content of all of the files. So far I am able to access the directory and list the files.
#!/bin/bash
#Check for folder name
if [ "$#" -ne 1 ]; then
echo " Usage: count [folder name]"
exit 1
fi
#Check if it is a directory
if [ ! -d "$1" ]; then
echo "Not a valid directory"
exit 2
fi
#Look at the directory
target=$1
echo "In Folder: $target"
for entry in `ls $target`; do
echo $entry
done
So if I use the command ./display [directory] it will list the files. I want to display the contents of all of the files as well but I am stuck. Any help would be appreciated thanks!
Use find to find files. Use less to display files interactively or cat otherwise.
find "$target" -type f -exec less {} \;
I thin a loop similar to your "look at the directory" loop would suffice, but using the cat command instead of ls

Check if there's some file on a directory regardless of another directory inside - linux ksh

I'm trying to check if there's any file on a directory but i need it to be independent of the fact that can or cannot be another directory inside.
I'm using:
if [ -n "$(ls -A /unload/ebia 2>/dev/null)" ]
then
Exists="Yes"
else
Exists="No"
fi
echo "is any file inside $PATH ? $Exists."
If there's nothing on $PATH, it say no, and if there's any file inside, it say yes (it's correct), but if i create a directory inside $PATH, it keeps answering yes instead there's no file. How to avoid this?
use find:
if [ -n "$(find /unload/ebia/ -type f)" ]
then
Exists="Yes"
else
Exists="No"
fi
finds -type f switch only searches for files and skips directorys

using IF to see a directory exists if not do something

I am trying to move the directories from $DIR1 to $DIR2 if $DIR2 does not have the same directory name
if [[ ! $(ls -d /$DIR2/* | grep test) ]] is what I currently have.
then
mv $DIR1/test* /$DIR2
fi
first it gives
ls: cannot access //data/lims/PROCESSING/*: No such file or directory
when $DIR2 is empty
however, it still works.
secondly
when i run the shell script twice.
it doesn't let me move the directories with the similar name.
for example
in $DIR1 i have test-1 test-2 test-3
when it runs for the first time all three directories moves to $DIR2
after that i do mkdir test-4 at $DIR1 and run the script again..
it does not let me move the test-4 because my loop thinks that test-4 is already there since I am grabbing all test
how can I go around and move test-4 ?
Firstly, you can check whether or not a directory exists using bash's built in 'True if directory exists' expression:
test="/some/path/maybe"
if [ -d "$test" ]; then
echo "$test is a directory"
fi
However, you want to test if something is not a directory. You've shown in your code that you already know how to negate the expression:
test="/some/path/maybe"
if [ ! -d "$test" ]; then
echo "$test is NOT a directory"
fi
You also seem to be using ls to get a list of files. Perhaps you want to loop over them and do something if the files are not a directory?
dir="/some/path/maybe"
for test in $(ls $dir);
do
if [ ! -d $test ]; then
echo "$test is NOT a directory."
fi
done
A good place to look for bash stuff like this is Machtelt Garrels' guide. His page on the various expressions you can use in if statements helped me a lot.
Moving directories from a source to a destination if they don't already exist in the destination:
For the sake of readability I'm going to refer to your DIR1 and DIR2 as src and dest. First, let's declare them:
src="/place/dir1/"
dest="/place/dir2/"
Note the trailing slashes. We'll append the names of folders to these paths so the trailing slashes make that simpler. You also seem to be limiting the directories you want to move by whether or not they have the word test in their name:
filter="test"
So, let's first loop through the directories in source that pass the filter; if they don't exist in dest let's move them there:
for dir in $(ls -d $src | grep $filter); do
if [ ! -d "$dest$dir" ]; then
mv "$src$dir" "$dest"
fi
done
I hope that solves your issue. But be warned, #gniourf_gniourf posted a link in the comments that should be heeded!
If you need to mv some directories to another according to some pattern, than you can use find:
find . -type d -name "test*" -exec mv -t /tmp/target {} +
Details:
-type d - will search only for directories
-name "" - set search pattern
-exec - do something with find results
-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
There are many examples of exec or xargs usage.
And if you do not want to overwrite files, than add -n option to mv command:
find . -type d -name "test*" -exec mv -n -t /tmp/target {} +
-n, --no-clobber do not overwrite an existing file

Shell Script: Copy directory passed as variable to another directory also as variable passed by user

SHELL SCRIPT:
How can i copy a directory passed as a variable by user to another directory also as variable?
I mean, the user type a source directory and the destination. So, the files into directory are copied to the other directory.
if cp -R "$source" "$destination"
then echo "Copy successful :)"
else echo "Copy failed :("
fi
The -R option to cp specifies that it should copy the directory recursively. $source should contain the original directory, and $destination should contain the location where you want it copied.
If you want to do something like this:
$ ./script.sh file1.ext file2.ext
The script.sh could be:
#!/bin/bash
cp -rv "$0" "$1"
Hope it helps.

listing files from directory by passing command line arguments

I am trying to list files from any number of directories by using command line arguments.
I am trying to pass the directory paths through the command line and display files that are in searched directories. Something like this.
"Directory: PATH1"
files
files
files
"Directory: PATH2"
files
files
etc.
So I am using $* to pass all of the command line arguments but it only displays files from the first listed directory.
#!/bin/bash
cd $*
for filename in *
do
echo "Directory: $*"
echo $filename
done
Just in case you have a space in a directory name you might want to use "$#", and it doesn't hurt to check that a directory exists:
for dir in "$#" ; do
if [ -d "$dir" ] ; then
echo "Directory: $dir"
ls "$dir" # If all you want is to show the contents, this should do
else
echo "Not a directory: $dir"
fi
done
Something similar to this should work:
dirs=$*
for dir in $dirs
do
echo "Directory: $dir"
for filename in $(ls $dir)
do
echo $filename
done
done

Resources