Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)

I typically do:

tar -czvf my_directory.tar.gz my_directory

I decided to include everything in the my_directory, but not this directory itself? I don't want:

my_directory

   --- my_file

   --- my_file

   --- my_file

I want:

my_file

my_file

my_file

1 Answer

0 votes
by (36.8k points)

Use the below command: 

cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd - 

This should do your job in one line. It also works well on hidden files as well. "*" doesn't expand hidden files by path name expansion at least in bash. Below is my experiment:

$ mkdir my_directory

$ touch my_directory/file1

$ touch my_directory/file2

$ touch my_directory/.hiddenfile1

$ touch my_directory/.hiddenfile2

$ cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..

./

./file1

./file2

./.hiddenfile1

./.hiddenfile2

$ tar ztf my_dir.tgz

./

./file1

./file2

./.hiddenfile1

./.hiddenfile2

To know about Linux join the Linux training 

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...