Linux - Creating multiple folders from a text file - linux

#!/bin/bash
IFS='
'
for _dir in $(cat "$1 usernames.lnk"); do
mkdir "$_dir Windows"
done
I am having trouble with this script, It has to create a folder within the Windows folder of each name that is listed in the usernames.lnk text file, but everytime I try and run the script it states "usernames.lnk" directory could not be found. The script and the username file and the Windows folder are all in the same folder. Thanks in advance.

This will read each line of the file usernames.lnk and create a directory under the directory Windows (relative path).
#!/bin/bash
while read -r line; do
mkdir "Windows/$line"
done < usernames.lnk

This way should work:
#!/bin/bash
for i in `cat usernames.lnk`;
do mkdir "Windows/$i";
done

Related

Copy a file from a directory and paste it to multiple sub directories in linux(ubuntu) terminal?

I have a directory mnt/d/LIVI.
Inside the directory, LIVI, I have sub-directories:
mnt/d/LIVI/ak
mnt/d/LIVI/ag
mnt/d/LIVI/few
mnt/d/LIVI/ww4
mnt/d/LIVI/ks5
I wanted to copy a file named tt.txt from mnt/d/LIVI/ak/tt.txt and paste to all the sub directories of LIVI, using Ubuntu terminal. How do i do it using a shell script file?
I tried the following one, but it didn't work.
I created a text file named mnt/d/LIVI/FOLDERS.txt, This listed all the sub directories names.
And saved a script file in mnt/d/LIVI directory. The following is the script
#!/bin/sh
# -*- coding: utf-8-unix -*-
ROOTDIR=$(dirname $0 | xargs readlink -f)
for SIMDIR in cat FOLDERS.txt | xargs readlink -f ; do
cp mnt/d/LIVI/ak/tt.txt $SIMDIR
done
#cd ..
date
You may try this bash script
#!/bin/bash
cd "${0%/*}" || exit
for dir in */; do
if [[ $dir != 'ak/' ]]; then
cp ak/tt.txt "$dir"
fi
done
The script must reside under the diectory mnt/d/LIVI
Don't read lines with for.
(If you really wanted to, the syntax for that would look like
for dir in $(cat FOLDERS.txt); do
...
but really, don't. The link above explains why in more detail.)
I don't see why you want to run readlink on the directories at all?
cd "$(dirname "$0")"
while read -r dir; do
cp ak/tt.txt "$dir"
done <FOLDERS.txt
Note also Correct Bash and shell script variable capitalization

Having trouble implementing cp -u in shell script

For a school project, I have a shell script that is supposed to copy the files in two directories (without looking at subdirectories) into a third directory. I'm testing out the -u command so that if two files have the same name, only the newer one will get copied over (that's also a spec). My shell script looks like this (excluding #! and error checking):
cd $1 #first directory
for file in `ls`; do
if [ -f $file ]; then
cp "$file" ../$3 # $3 is the third directory
fi
done
cd ../$2
for file in `ls`; do
if [ -f $file ]; then
cp -u "$file" ../$3
fi
done
My current shell script will copy files that don't exist in directory 3 already, and it won't overwrite a newer file with an older file with the same name. However, my shell script doesn't overwrite an older file with a newer file of the same name in directory 3. I don't think there's anything wrong with the -u command. Can you help find the bug in my code? Thanks!
You are missing the -u option in the first loop:
cp "$file" ../$3 # $3 is the third directory
should instead read:
cp-u"$file" ../$3 # $3 is the third directory

Bash script, redirect output to another directory

I have an environment variable containing the name of a directory. I am trying to redirect output from an echo command to a text file in a different directory.
For example
DIR="NewDirectory"
mkdir $DIR
echo "Testing" >> "$DIR\file.txt"
Results in a file named NewDirectory\file.txt in the working directory of the script...what exactly am I missing here? The directory is created without issue, so I am not sure what is going on here.
You have to change \ into /:
DIR="NewDirectory"
mkdir -p $DIR
echo "Testing" >> "$DIR/file.txt"
Changed mkdir -p as suggested by #Jord, because -p means: no error if existing, make parent directories as needed
In linux (or unix for that matter), the directory separator is a slash (/), not a backslash (\):
DIR="NewDirectory"
mkdir $DIR
echo "Testing" >> "$DIR/file.txt"
Your line
echo "Testing" >> "$DIR\file.txt"
should read
echo "Testing" >> "$DIR/file.txt"
as / is the separator in paths in Linux.

linux bash script to create folder and move files

Hello I need to create folder based on a filename and in this folder create another one and then move file to this second folder
example:
my_file.jpg
create folder my_file
create folder picture
move my_file.jpg to picture
I have this script but it only works on windows and now I'm using Linux
for %%A in (*.jpg) do mkdir "%%~nA/picture" & move "%%A" "%%~nA/picture"
pause
Sorry if I'm not precise but English is not my native language.
#!/usr/bin/env bash
# Enable bash built-in extglob to ease file matching.
shopt -s extglob
# To deal with the case where nothing matches. (courtesy of mklement0)
shopt -s nullglob
# A pattern to match files with specific file extensions.
# Example for matching additional file types.
#match="*+(jpg|.png|.gif)"
match="*+(.jpg)"
# By default use the current working directory.
src="${1:-.}"
dest="${2:-/root/Desktop/My_pictures/}"
# Pass an argument to this script to name the subdirectory
# something other than picture.
subdirectory="${3:-picture}"
# For each file matched
for file in "${src}"/$match
do
# make a directory with the same name without file extension
# and a subdirectory.
targetdir="${dest}/$(basename "${file%.*}")/${subdirectory}"
# Remove echo command after the script outputs fit your use case.
echo mkdir -p "${targetdir}"
# Move the file to the subdirectory.
echo mv "$file" "${targetdir}"
done
Use basename to create the directory name, mkdir to create the folder, and mv the file:
for file in *.jpg; do
folder=$(basename "$file" ".jpg")"/picture"
mkdir -p "$folder" && mv "$file" "$folder"
done
Try the following:
for f in *.jpg; do
mkdir -p "${f%.jpg}/picture"
mv "$f" "${f%.jpg}/picture"
done
${f%.jpg} extracts the part of the filename before the .jpg to create the directory. Then the file is moved there.

Copying syslog file to a new directory in linux

I'm currently having an assignment to write a bash script that can perform backup log (syslog, dmesg and message) files to a new directory. I wrote my script like this:
cd /var/log
sudo cp syslog Assignment
The file "Assignment" is in my home directory. When I used the "ls" command in my Assignment folder, I don't find a copy of syslog in there. Can someone tell me where did I go wrong? Thanks in advance.
I think you mean Assignment folder, not Assignment file. Anyways if you cd to /var/log, then when you do a cp in /var/log it will think Assignment is local to /var/log. If you do an ls in /var/log now you will see a copy of syslog called Assignment in /var/log. To get syslog copied to the assignment folder in your home directory you need to specify the absolute path not the relative path. Use the tilde, ~, to specify the home directory. So your script should say
cd /var/log
sudo cp syslog ~/Assignment/
You can try this:
#!/bin/sh
if ! [ $1 ] ; then
echo "Usage:";
echo $0 "<directory_where_to_save_logs>";
return;
fi
if [ ! -d "$1" ]; then
echo "Creating directory $1";
mkdir $1;
fi
cp /var/log/syslog* $1
cp /var/log/dmesg* $1
Thanks

Resources