bash copy the first file form source folder to destination folder - linux

I am using mac os. I am in a situation that I need to copy the only first file private key which ends with _sk to another directory. Please any suggestion would help me.

You could loop over the files ending with _sk and break the loop after copying the first file. If that's really the "first file" you wanted to copy is another question.
for i in /path/to/source/*_sk; do cp "$i" /path/to/destination/; break; done

Related

How do I make a copy of one directory files(hidden files included) into another directory?

I'm trying to make a copy of one directory files into another directory.
I have Desktop/projectOne and Desktop/projectTwo and I'm trying to copy projectOne files into projectTwo. I need to use terminal for this as I need to copy hidden files also and I'm not familiar with linux commands...
So my question is...
What commands do I have to use to copy all files (hidden files included) from Desktop/projectOne to Desktop/projectTwo?
What commands do I have to use to copy only hidden files from Desktop/projectOne to Desktop/projectTwo?
Thanks in advance.
cp -r
Example: cp -r /oldfolder /home/newfolder
Noticed: if newfolder is already exist it will create new folder in it
/home/newfolder/oldfolder

Bash, copy files from folder to another

I have a problem with an Uni OS Course assignment.
Basically the task says:
Deliver now a file for assessment. The content of the file is: one line, containing a command that
copies all files with prefix "2016", from directory "ExercisesOS" to directory "OSLab".
Consider the current directory to be "~" when writing such command.
I have already tried with that code:
cp /ExercisesOS/2016* /OSLab
but it performs me two error.
How can I write the correct command?
You probably want to copy from the directory you are working.
To check where you are working:
$ pwd
/home/userdir
To copy from your working directory:
$ cp ExerciseOS/2016* OSLab/
mkdir OSLab && cp /ExercisesOS/2016* OSLab
This solution would assume that the directory 'OSLab' isn't already created.

Move files to different directories based on file name tokens

I am looking to write a script to move files from a directory:
/home/mydir/
To another directory based on tokens in the file name. I have a bunch of files named as such:
red_office_mike_2015_montreal_546968.ext
or
$color_$location_$name_$year_$city_$numbers.extension (files will be various movie files: mov, mp4, mkv, etc.)
I would like the script to move the files to the following location:
/dir/work/$color/$name
Then verify the file has successfully copied, and delete the original file once it has.
I would also love it if the script would create the to directory if it does not already exist.
So in summary, I need a script to move files based on underscore separated tokens, create the to directory if it doesn't already exist, verify the successful copy (maybe with a size check), then delete the original file.
I am working on linux, and would prefer a bash script. The variables I have given are generic, and I will incorporate some other things to the script, I'm just looking for help on building the skeleton.
Thanks in advance for any help!
It's not a bash script, but perl is much better at this kind of thing and is installed on all Linux systems
while(<>) {
chomp;
$file = $_;
($colour, $location, $name, $year, $city, $numbers) = split(/_/,$file);
$dest0 = "/dir/work/$colour";
$dest1 = "$dest0/$name";
mkdir ($dest0) unless (-d $dest0);
mkdir ($dest1) unless (-d $dest1);
rename ($file, "$dest1/$file");
}
The script splits your input file on the underscore character, creates all the directories to the destination and then renames the file to the new filename. Rename takes care of all the copying and deleting for you. In fact it just changes the directory entries without any copying at all.
UPDATE
The above version takes its input from a file containing a list of filenames to process. For an alternative version which processes all files in the current directory, replace the while line with
while(glob("*")) {
I was able to fumble around online and come up with a for loop to do this task. I used cut and it made things simple. Here is what worked for me:
#!/bin/sh
cd "${1:-.}"
for f in *.*; do
color=`echo "$f" | cut -d'_' -f1`
name=`echo "$f" | cut -d'_' -f3`
todir="/dir/work/$color/$name"
mkdir -p "$todir"
mv "$f" "$todir"
done
This worked perfectly and I hope it can help others who might need to create directories based on portions of filenames.
The first line under the shebang made it so that it will either look at the current working directory or a directory you pass it as an argument.
Thanks to those who chimed in on the original post. I'm new with scripting so it take me a while to figure this stuff out. I love this site though, it is super helpful!

How to copy a specific file in a folder using terminal

I am using terminal on linux. I am in my current folder now. I want to take a file in this folder that I am currently in and copy it in the exact same folder and I also want to rename it.
What command should I use?
Copy command works well.
cp /currentfolder/filename /currentfolder/newfilename
If you wanna copy a file like .cpp file it is really easy to use:
cp filename.cpp destination

Linux bash script to copy files

I need script to copy on cron basis a list of files. Files selected on name/datetime pattern and to name of file destination must by appended data like ddmmyyy.
It is not problem copy files or directory, but problem to change name of each file according to its data. May be exists some open source solution?
Thanks.
You haven't provided enough information for me to give you real working code; but you can do something like this:
file=dated_log.log
ddmmyyyy=$(read -r < "$file" ; echo "${REPLY:1:8}")
cp "$file" "$file.$ddmmyyyy"
The above will copy dated_log.log to data_log.log.30102011, assuming that the first line of dated_log.log starts with 30102011.
The Bash Reference Manual will hopefully help you adjust the above to suit your needs.

Resources