0

I'm running Ubuntu on WSL2. I frequently download zipped homework files from my school's website. They go into my Downloads folder on Windows. I want them copied into a particular path on my Linux filesystem, unzipped, and then renamed to put my name in front of the filename.

This is what I have so far:

params: $1: filename, $2: week_x, $3: day_y
hwcopy() {
    cp $1 /home/myName/homework/$2/$3
    rm -r $1
    cd /home/myName/homework/$2/$3
    unzip $1
}

I have a function addname but the problem is if I call addname $1, that will just rename the zipped folder.

Here's a solution I thought of:

hwcopy() {
    cp $1.zip /home/myName/homework/$2/$3
    rm -r $1.zip
    cd /home/myName/homework/$2/$3
    unzip $1.zip
    addname $1
}

I suppose this would work, but it's a bit annoying because after autocompleting the file name for $1, I would need to hit backspace 4 times every time I wanted to call the function to get rid of the .zip in the argument. Is there a simple way to do this?

I'm new to Linux so I don't know how to do this but I was thinking maybe there's a way to save $1 as a string inside the function and then cut off the last 4 characters, and then pass that into everything else?

1 Answer 1

1

With bash, use substitution with % char to remove end of content:

$ file="myfile.zip"
$ echo "${file%.zip}"
myfile

You could use wildcard:

$ file="myfile.zip"
$ echo "${file%.*}"
myfile

You could maximize motif with double %:

$ file="myfile.tar.gz"
$ echo "${file%.*}"
myfile.tar
$ echo "${file%%.*}"
myfile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .